bit manipulation

This is a discussion on bit manipulation within the C Programming forums, part of the General Programming Boards category; hi, I have a structure like the following that I'm trying to assign characters to and send across the network. ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    14

    bit manipulation

    hi,

    I have a structure like the following that I'm trying to assign characters to and send across the network.

    Code:
    struct my_data {
        __be32 name; // this is a 4 byte field
        unsigned char version:8;
    };
    I was trying to assign the letters a, b, c, and d using bit shifting to these fields but don't beleive I have this correct. Can anyone help out here?

    Code:
        
        struct my_data *dat;
        // alloc data struct here ...
    
        dat->letA = (0x61 << 0);
        dat->letB = (0x62 << 0);
        dat->letC = (0x63 << 0);
        dat->letD = (0x64 << 0);
    
        dat->version = 0x01;

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Why do you think dat->letA exists? Or letB, or letC, or letD? Did you define __be32 as a union?

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,293
    Also, why do you have a :8 at the end of the declaration of version? It has no effect in this case.
    Are you using sizeof(my_data) anywhere? What value do you get and what value were you expecting?
    Please tell us what you are really trying to do.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Feb 2011
    Posts
    14
    Quote Originally Posted by tabstop View Post
    Why do you think dat->letA exists? Or letB, or letC, or letD? Did you define __be32 as a union?
    I'm sorry, that should read ...
    Code:
        dat->name = (0x61 << 0);
        dat->name = (0x62 << 0);
        dat->name = (0x63 << 0);
        dat->name = (0x64 << 0);
    so that this 4 byte should should end up as 'abcd'. I am just trying to send this across the network and make sure my order is correct so that it ends up at the other host as expected. In that snippet, I'm trying to efficiently assign characters to each field.

    And I just put the 8 there for readability, but I do see your point.
    Last edited by rrlangly; 07-16-2011 at 02:33 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Do you know what << does? Why are you always shifting by 0? You should know how many bits you want to move your character to get it in the right "place".

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You should be able to assign the characters like below, without any bit shifting.

    Code:
    dat->name = 'abcd';
    Keep byte order in mind though, you might want 'dcba'.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C Bit Manipulation
    By Sicilian_10 in forum C Programming
    Replies: 4
    Last Post: 03-19-2010, 10:01 AM
  2. bit manipulation
    By cblix in forum C Programming
    Replies: 6
    Last Post: 11-29-2005, 05:37 PM
  3. Bit manipulation
    By pkalluri in forum C++ Programming
    Replies: 5
    Last Post: 05-12-2003, 07:06 AM
  4. Bit manipulation
    By crag2804 in forum C Programming
    Replies: 3
    Last Post: 09-29-2002, 06:33 PM
  5. Help - Bit Manipulation
    By LivLuvLafLrn in forum C Programming
    Replies: 5
    Last Post: 06-06-2002, 02:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21