1 and 7 are bit sizes to limit the range of values. They are commonly found in structures and unions. For example, on some systems (depends on char width and packing rules, etc.) Code:
typedef struct { unsigned char a : 1; unsigned char b : 7; } tOneAndSevenBits;
creates an 8-bit value, one bit for a and 7 bits for b .
Commonly used in C to access “compressed” values, such as a 4-bit nybble, which can be contained in the upper half of an 8-bit char:
typedef struct { unsigned char leftFour : 4; unsigned char rightFour : 4; } tTwoNybbles;
For lawyers among us, section 9.6 of the C ++ 11 standard explains this in detail, slightly paraphrasing:
Bit fields [class.bit]
Form declaring member
identifier of <sub> automatic sub> attribute specifier of <sub> automatic sub>: constant expression
indicates a bit field; its length begins with a colon field name. The optional specifier attribute refers to the declared object. The bitfield attribute is not part of the class member type.
A constant expression must be an integral constant expression with a value greater than or equal to zero. The value of the integral expression of the constant may be greater than the number of bits in the representation of an object such as bit fields; in such cases, the extra bits are used as padding bits and do not participate in the representation of the bit field values.
The distribution of bit fields within a class object is determined by the implementation. The alignment of bit fields is determined by the implementation. Bit fields are packed in some address allocation units.
Note: block allocation blocks of bit blocks on some machines, not others. Bit fields are assigned from right to left on some machines, from left to right on others. - end of note
paxdiablo Oct 22 '09 at 4:09 2009-10-22 04:09
source share