Bits can be an efficient way of storing settings and flags.
- The
bitset
class - The
vector<bool>
std::bitset
is the STL class designed for handling information in bits and bit flags. std::bitset
is not an STL container class because it cannot resize itself.
To use class std::bitset
, include header: #include <bitset>
std::bitset<4> fourBits; // 4 bits initialized to 0000
std::bitset<5> fiveBits("10101"); // 5 bits 10101
std::bitset<6> sixBits(0b100001); // C++14 binary literal
std::bitset<8> eightBits (255); // 8 bits initialized to long int 255
std::bitset<8> eightBitsCopy(eightBits);
This number is fixed at compile time; it isn’t dynamic. You can’t insert more bits into a bitset
like you did to a vector
.
The prefix
0b
or0B
tells the compiler that the following digits are a binary representation of an integer. This literal is new to C++ and introduced in C++14.
Just a vector
of boolean
type elements, besides the vector<bool>
features the function flip()
similar to the function of bitset<>::flip()
.
To use class std::vector<bool>
, include header: #include <vector>
.
vector<bool> boolFlags1;
vector<bool> boolFlags{ true, true, false };
vector<bool> boolFlags2 (10, true);
vector<bool> boolFlags2Copy (boolFlags2);