Bit Fields in Structures
C++, like C, enables you to specify structure members that occupy a particular number of bits. This can be handy for creating a data structure that corresponds, say, to a register on some hardware device. The field type should be an integral or enumeration type (enumerations are discussed later in this chapter), and a colon followed by a number indicates the actual number of bits to be used. You can use unnamed fields to provide spacing. Each member is termed a bit field. Here’s an example:
struct torgle_register{ unsigned int SN : 4; // 4 bits for SN value unsigned int : 4; // 4 bits unused bool goodIn : 1; // valid input (1 bit) bool goodTorgle : 1; // successful torgling};
You can initialize ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access