Bit-fields

suggest change

A bit-field is a structure or union member declared with a width in bits:

struct status {
    unsigned ready : 1;
    unsigned mode : 3;
    unsigned : 0;       /* Start a new allocation unit. */
    unsigned error : 1;
};

The general form is:

type-specifier identifier : width

The identifier may be omitted to create an unnamed bit-field. A width of zero is permitted only for an unnamed field; it prevents the next bit-field from sharing the current allocation unit. A named field's width must not exceed the number of bits in the type it would have without the bit-field declarator.

The portable type choices in C11 and C17 are _Bool, signed int, unsigned int, or another implementation-defined type. Whether a plain int bit-field is signed is implementation-defined. C23 additionally supports bool and bit-precise integer types, subject to the rules for those types.

Allocation order, alignment, whether a field can cross an allocation-unit boundary, and several other layout details are implementation-defined. Do not use bit-fields as a portable representation for file formats, network protocols, or hardware registers unless the implementation's layout rules are part of the contract.

The unary & operator cannot be applied to a bit-field, so a bit-field has no independently addressable pointer. Whether an atomic type is permitted for a bit-field is implementation-defined; portable code should not rely on it.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents