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 ...

Get C++ Primer Plus now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.