Page 64 - C-Language
P. 64
Chapter 7: Bit-fields
Introduction
Most variables in C have a size that is an integral number of bytes. Bit-fields are a part of a
structure that don't necessarily occupy a integral number of bytes; they can any number of bits.
Multiple bit-fields can be packed into a single storage unit. They are a part of standard C, but there
are many aspects that are implementation defined. They are one of the least portable parts of C.
Syntax
• type-specifier identifier : size;
Parameters
Parameter Description
type-specifier signed, unsigned, int or _Bool
identifier The name for this field in the structure
size The number of bits to use for this field
Remarks
The only portable types for bit-fields are signed, unsigned or _Bool. The plain int type can be used,
but the standard says (§6.7.2¶5) … for bit-fields, it is implementation-defined whether the specifier
int designates the same type as signed int or the same type as unsigned int.
Other integer types may be allowed by a specific implementation, but using them is not portable.
Examples
Bit-fields
A simple bit-field can be used to describe things that may have a specific number of bits involved.
struct encoderPosition {
unsigned int encoderCounts : 23;
unsigned int encoderTurns : 4;
unsigned int _reserved : 5;
};
In this example we consider an encoder with 23 bits of single precision and 4 bits to describe
https://riptutorial.com/ 40

