Thread: Bit fields

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Bit fields

    I have a bit field struct as below

    Code:
    typedef struct
    {
    	uint32_t				bB0:1;	
    	uint32_t				bB1:1;	
    	uint32_t				bB2:1;	
    	uint32_t				bB3:1;	
    	uint32_t				bB4:1;	
    	uint32_t				bB5:1;	
    	uint32_t				bB6:1;	
    	uint32_t				bB7:1;	
    	uint32_t				bB8:1;	
    	uint32_t				bB9:1;	
    	uint32_t				bB10:1;	
    	uint32_t				bB11:1;	
    	uint32_t				bB12:1;	
    	uint32_t				bB13:1;	
    	uint32_t				bB14:1;	
    	uint32_t				bB15:1;	
    	uint32_t				bB16:1;	
    	uint32_t				bB17:1;	
    	uint32_t				bB18:1;	
    	uint32_t				bB19:1;	
    	uint32_t				bB20:1;	
    	uint32_t				bB21:1;	
    	uint32_t				bB22:1;	
    	uint32_t				bB23:1;	
    	uint32_t				bB24:1;	
    	uint32_t				bB25:1;	
    	uint32_t				bB26:1;	
    }channels;
    but there are only 26 bit fields and it is a size of 32 bits, I am wondering will there be any problem?

    Is there a faster way of assigning values to the bit fields? other than channels.bB1 = 1 and so on and so forth?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you are just assigning one bit field to another, it should work fine. It doesn't matter if there is padding in the structure. Now if you flat write it to a file, take it over to some other system, and flat read it back in, you might not get what you wrote out, but other than that you should be fine.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    Quote Originally Posted by quzah View Post
    If you are just assigning one bit field to another, it should work fine. It doesn't matter if there is padding in the structure. Now if you flat write it to a file, take it over to some other system, and flat read it back in, you might not get what you wrote out, but other than that you should be fine.


    Quzah.
    hmm. This will be passed as a byte stream and on the other side, read the byte stream as the same as above

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Then things could get a little sticky, unless you are careful about endian issues.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    if its big endian, how do i go about assigning values to it?

    just like this?

    Code:
     channels.bB1 = 1
    if i want to pass this struct as a byte stream, do I do this

    Code:
    (char_t*)channels
    Not sure about the padding of 6 bits to fulfill the 32 bit size. =(

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > if its big endian, how do i go about assigning values to it?
    > just like this?
    > channels.bB1 = 1
    Yes, and that has nothing to do with what endian the machine is, or how the compiler decided to pack the bits.

    > if i want to pass this struct as a byte stream, do I do this
    > (char_t*)channels
    Not if you want the code to be portable.
    To be portable, you have to unpack the bit field structure one member at a time, and repack in a known format.

    So something like
    unsigned long sendthis = 0;
    sendthis = channels.bB1 | ( channels.bB2 << 1 )
    and so on.

    Bit fields are pretty much a legacy relic from the past. It sounds good, but almost everything about how they are stored is implementation specific. Which means they're all but useless if you intend to use them to communicate data with something outside the program.
    Their real utility stems from the days when C was first invented, and say 64KB of memory was considered a luxury. Packing data into as few bits as possible was a good thing.

    I think you would be better off with just having an unsigned long, and a few macros like
    Code:
    #define SETBIT(var,pos) var |= (1ul << pos )
    #define CLRBIT(var,pos) var &= (~(1ul<<pos))
    #define GETBIT(var,pos) ( var >> pos ) & 1
    Which might actually be a bit simpler than having 20+ separate names for things.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bit fields
    By Perspektyva in forum C++ Programming
    Replies: 13
    Last Post: 11-22-2008, 02:38 PM
  2. bit fields before non-bit fields . . .
    By dwks in forum C Programming
    Replies: 10
    Last Post: 10-13-2005, 02:36 AM
  3. bit-fields
    By kps in forum C Programming
    Replies: 9
    Last Post: 09-25-2002, 08:25 PM
  4. help with adding fields
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 04-22-2002, 07:15 PM
  5. Bit fields
    By GaPe in forum C Programming
    Replies: 8
    Last Post: 01-22-2002, 02:01 PM