20.6. Bit Fields

C++ provides the ability to specify the number of bits in which an integral type or enum type member of a class or a structure is stored. Such a member is referred to as a bit field. Bit fields enable better memory utilization by storing data in the minimum number of bits required. Bit field members must be declared as an integral or enum type.

Image Performance Tip 20.1

Bit fields help conserve storage.

Consider the following structure definition:

struct BitCard{   unsigned face : 4;   unsigned suit : 2;   unsigned color : 1;}; // end struct BitCard

The definition contains three unsigned bit fields—face, suit and color—used to ...

Get C++11 for Programmers, Second Edition now with O’Reilly online learning.

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