Possible Duplicate:
What does ‘unsigned temp:3’ mean?
I came across one syntax like int variable:4;
can anyone tell me what this syntax means?
struct abc
{
int a;
int b:2;
int c:1;
};`enter code here`
Possible Duplicate:
What does ‘unsigned temp:3’ mean?
I came across one syntax like int variable:4;
can anyone tell me what this syntax means?
struct abc
{
int a;
int b:2;
int c:1;
};`enter code here`
It defines a width of a bitfield in a struct. A bitfield holds an integer value, but its length is restricted to a certain number of bits, and hence it can only hold a restricted range of values.
In the code you posted, in the structure a
is a 32-bit integer, b
is a 2-bit bitfield and c
is a 1-bit bitfield.
It is a bit-field. Instead of storing a full integer for b it only stores 2 bits, so b can have values of -2, -1, 0 and 1. Similarly c can only have values of -1 and 0.
Depending on what version of the compiler you have the sign extension is a little unpredictable, and some systems may present these values as 0, 1 2 and 3 or 0 and 1.
This will also pack the fields into less than an integer, but again, this is in an implementation defined manner, and you would do well not to make assumptions about how much memory will actually be used or the order of the data in memory.