As commented above, it's the number of bits to be used for each field.
struct MYMSG
{
unsigned short src : 4; // allows values 0 - 15
unsigned short dst : 11; // allows values 0 - 2047
unsigned short tx : 1; // allows values 0 - 1
};
This also has the effect of packing the structure if alignment is turned off. If this structure is not padded, then a call to sizeof()
will return 2 (on an 8-bit/byte architecture).
In this case, a single unsigned short
is allocated, and the bit fields are divided up within that value. Setting a value outside the range of one of the fields (such as 16
for src
) will cause an overflow of that particular field, but will not alter the values of any other fields (the value of dst
will not change).
Take a slightly more obtuse example:
struct T
{
unsigned long val : 4
};
This still allocates a full unsigned long
(32bit on most architectures), but only allows for setting of the first 4
bits giving valid values of 0
-15
.