Bit-field Structures

Bit-fields may be defined within any structure declaration, for example:

struct [struct_name]
{
type [element_name] : number_of_bits;
type [element_name] : number_of_bits;
type [element_name] : number_of_bits;
} [var_name, var_name] ;

where the optional var_names are variables declared to be of this structure type, and the optional struct_name is a name for this structure type which may be used in subsequent variable declarations of the form:

struct struct_name var_name;

The type may only be a signed or unsigned short int. The element_name may be omitted if those bits are unused. The number_of_bits is a constant value.

This declares one or more bytes, enough to hold the total of the number_of_bits listed. Each element then refers to a particular bit (or bits) within that byte (or bytes). The bit-fields are generally assigned starting from the least-significant end of the byte, although this is implementation dependent.