What does the colon mean in a structure declaration, for example: 1 ,: 7 ,: 16, or: 32?

What does the following C ++ code mean?

unsigned char a : 1; unsigned char b : 7; 

I assume that it creates two char a and b, and both of them should be one byte long, but I have no idea what the ": 1" and ": 7" parts do.

+29
c ++ c bit-fields
Oct 22 '09 at 4:08
source share
3 answers

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

+46
Oct 22 '09 at 4:09
source share

I think it will be beatpods.

+7
Oct 22 '09 at 4:09
source share

Strictly speaking, the bit field must be int, unsigned int, or _Bool. Although most compilers will use any integral type.

Ref C11 6.7.2.1:

The bit field must be of a type that is a qualified or unqualified version of _Bool, signed int, unsigned int, or some other type of implementation.

Your compiler will probably allocate 1 byte of storage, but it will be able to capture more.

Ref C11 6.7.2.1:

An implementation can allocate any addressable storage unit large enough to hold a bit field.

Savings occur when you have several bitfields declared one by one. In this case, the dedicated storage will be packed if possible.

Ref C11 6.7.2.1:

If there is enough space left, a bit field that immediately follows another bit field in the structure should be packed into adjacent bits of the same block. If there is not enough space, will there be a bit field that does not fit, is placed in the next block or the overlap of adjacent blocks is determined by the implementation.

0
Jul 21 '16 at 15:34
source share



All Articles