Excerpt from "The C Programming Language" Although these idioms are readily mastered, as an alternative C offers the capability of defining and accessing fields within a word directly rather than by bitwise logical operators. A bit-field, or field for short, is a set Of adjacentation-defined Storage Unit That We Will Call a `` Word. 'FOR EXAMPLE, The Symbol Table #defines Above Could Be Replaced by The Definition of Three Fields:
Struct {unsigned int is_keyword: 1; unsigned int is_extern: 1; unsigned int is_static: 1;} Flags
This defines a variable table called flags that contains three 1-bit fields. The number following the colon represents the field width in bits. The fields are declared unsigned int to ensure that they are unsigned quantities. Individual fields are referenced in the same way as other structure members:. flags.is_keyword, flags.is_extern, etc. Fields behave like small integers, and may participate in arithmetic expressions just like other integers Thus the previous examples may be written more naturally as
Flags.is_extern = flags.is_static = 1;
To Turn The Bits ON; Flags.is_Extern = Flags.is_Static = 0;
To Turn Them Off; and if (Flags.is_Extern == 0 && Flags.is_Static == 0) ...
to test them Almost everything about fields is implementation-dependent Whether a field may overlap a word boundary is implementation-defined Fields need not be names;.... unnamed fields (a colon and width only) are used for padding The special width 0 May Be used to force alignment at the next word boundary.