Bitfields

Started by
4 comments, last by Wytter 18 years, 8 months ago
Hello, I recently rediscovered how to use memory efficiently in c when using "smaller than BYTE"-types: unsigned char twoBits : 2; unsigned char oneBit : 1; unsigned char fiveBits : 5 The above will take just one BYTE in memory (oneBYTE), right? Anyway, my question is following: How does c handle those types? If I write following: if (oneBit) { ... } Will the compiler transform this into somthing like: if (oneBYTE&0x04) { ... } Or if I write: fiveBits=anyByteValue; will the compiler transform this into: oneBYTE=(oneBYTE&0xf8)|(anyByteValue<<3); or will it overwrite the other bits too? Thanks :)
Advertisement
The easiest way to get an answer is to ask your compiler. The /FAs switch for MSVC or the -S switch for gcc.
as sicare said check out the assambler code he generates

i guess he does a comparsion with 0

the comparsion is done with a subtraction
and sets some flags (don t know their name right now)
http://www.8ung.at/basiror/theironcross.html
Thanks for the replies,

In fact I didn't express myself very well ^^;
What I wanted to ask:

Is the code that I wrote giving me the result I expect? ie. I don't care too much about how it is done internally, but in following case:

twoBits=0;
oneBit=0;
fiveBits=anyByteValue;

will my oneBYTE be

a) (anyByteValue&0x1f)<<3
b) (anyByteValue&0xf8)

Thanks
Just use it as a regular unsigned int:
unsigned a : 3;unsigned b : 3;b = 2;unsigned int x = 12;a = x;// b is still 2// a is 12 % (1 << 3) = 12 & 7 = 4

It masks the bits is the same way as:
unsigned char x;x = 12347; // = 12347 & 0xff = 59

Writing:
unsigned char twoBits : 2;unsigned char oneBit : 1;unsigned char fiveBits : 5

Doesn't ensure that twoBits, oneBit and fiveBits end's up in the same byte I think.

So doing:
union Test{  unsigned char theByte;  struct  {    unsigned char twoBits : 2;    unsigned char oneBit : 1;    unsigned char fiveBits : 5  };};

Doesn't mean that sizeof(Test) is one byte (although it probably is).
So doing:
Test a;  a.twoBits = 2;  a.oneBit = 1;  a.fiveBits = 5;Test b;  b.theByte = a.theByte; // Saving the union or whatever

Now b.twoBits might be different from a.twoBits.

[Edited by - eq on July 25, 2005 6:14:45 AM]
Using bitfields won't work like that. It'll take 3*sizeof(int) bytes into memory. You will have to use a union:

int main(int argc, char ** argv){        union {                unsigned char byte;                struct {                        unsigned char bit1:1;                        unsigned char bit2:1;                        unsigned char bit3:1;                        unsigned char bit4:1;                        unsigned char bit5:1;                        unsigned char bit6:1;                        unsigned char bit7:1;                        unsigned char bit8:1;                } bits;        } test;        if(argc!=2){                printf("Usage: %s <value>\n",argv[0]);                return -1;        }        test.byte=atoi(argv[1])&0xff;        printf("%d %d%d%d%d%d%d%d%d\n",test.byte, test.bits.bit1, test.bits.bit2, test.bits.bit3, test.bits.bit4, test.bits.bit5, test.bits.bit6, test.bits.bit7, test.bits.bit8);        return 1;}


When setting the bit field values directly, they will not overwrite the other values. So you can set test.bits.bit3 to 55 if you want, and still it'll only show test.bits.bit3 as 1 and the remaining as 0 (if initiated as 0 that is).
Nemo enim fere saltat sobrius, nisi forte insanit.

This topic is closed to new replies.

Advertisement