The class is very similar to a regular array, but optimizing for space allocation: each element occupies only one bit (which is eight times less than the smallest elemental type in C++: char).
Each element (each bit) can be accessed individually: for example, for a given bitset named mybitset, the expression mybitset accesses its fourth bit, just like a regular array accesses its elements.
Because no such small elemental type exists in most C++ environments, the individual elements are accessed as special references which mimic bool elements:
Apart from overriding several operators and to provide direct access to the bits, bitsets have the feature of being able to be constructed from and converted to both integer values and binary strings (see constructor, bitset::to_ulong and bitset::to_string). They can also be directly inserted and extracted from streams in binary format.
Bitsets have a fixed size. For a similar container class that also optimizes for space allocation and allows for dynamic resizing, see the bool specialization of vector (vector<bool>).
In their implementation in the C++ Standard Template Library, bitsets take a single template parameter:
Where the template parameter has the following meaning:
- N: Number of bits to contain (size_t is an integral type).
|(constructor)||Construct bitset (public member function)|
|applicable operators||Bitset operators (functions)|
|operator||Access bit (public member function)|
|set||Set bits (public member function)|
|reset||Reset bits (public member function)|
|flip||Flip bits (public member function)|
|to_ulong||Convert to unsigned long integer (public member function)|
|to_string||Convert to string (public member function)|
|count||Count bits set (public member function)|
|size||Return size (public member function)|
|test||Return bit value (public member function)|
|any||Test if any bit is set (public member function)|
|none||Test if no bit is set (public member function)|