CSE 202: Computer Science II, Winter 2018
std::bitset

Everything mentioned on this page can be referenced at The cppreference page for std::bitset

The std::bitset class template conceptually stores a set of bits and provides an interface for accessing and manipulating those bits. If you ever need to group a set of true/false values into one object, bitset should be used.

To use std::bitset, <bitset> should be included and std::bitset must be instantiated with the number of bits to store; the type std::bitset<32> is a bitset that stores 32 bits. This value is fixed and you must know how many bits you will use in the bitset before compilation.

A bitset can be default constructed or it may be explicitly constructed from an unsigned long int value or an std::string value.

bitset operators

The following program demonstrates the 3 bitwise operators that can be used with bitset:

#include <bitset> #include <iostream> using namespace std; int main() { bitset<4> b1("0110"); bitset<4> b2("0011"); cout << "b1: " << b1 << endl; cout << "b2: " << b2 << endl; cout << "b1 & b2: " << (b1 & b2) << endl; cout << "b1 | b2: " << (b1 | b2) << endl; cout << "b1 ^ b2: " << (b1 ^ b2) << endl; return 0; }

Bitsets, b1 and b2, with size 4 are direct-initialized with strings "0110" and "0011", respectively. A bitset can be inserted to an std::ostream and it's contents will be written in text form as a string of '0's and '1's.

The 3 bitwise operators that can be used between 2 bitsets are: logical AND (&), logical OR (|), and logical XOR (^). Each bitwise operator for std::bitset compares the individual bits between the 2 bitsets, column by column; each comparison results in a new bit value that will be part of the new bitset result of the expression.

For each comparison using the bitwise AND operator: if both bits equal 1, then the resulting bit equals 1, otherwise the resulting bit equals 0; Therefore, 0110 & 0011 results in 0010.

For each comparison using the bitwise OR operator: if at least one bit equals 1, then the resulting bit equals 1, otherwise the resulting bit equals 0; Therefore, 0110 | 0011 results in 0111.

For each comparison using the bitwise XOR operator: if both bits differ in value, then the resulting bit equals 1, otherwise the resulting bit equals 0; Therefore, 0110 ^ 0011 results in 0101.

The bitwise assignment operators: bitwise AND assignment (&=), bitwise OR assignment (|=), and bitwise XOR assignment (^=) perform the same operation as it's non-assignment equivalent while storing the result in the same variable used as the first operand.

There is another bitset operator, an unary operator called bitwise NOT (~) which inverts the value of each bit in the bitset.

Bitset element access

The subscript operator ([]) can be used to access a specific bitset bit through it's index. For a bitset with N elements, index 0 corresponds to the "least significant bit" and index N-1 corresponds to the "most significant bit".

For a bitset, b, represented with the string "01011": the most significant bit is '0', the first character in the string; the least significant bit is '1', the last character in the string. So b[0] = 1, b[1] = 1, b[2] = 0, b[3] = 1, and b[4] = 0.

One can count the number of bits set to 1 by calling the count member function. For our bitset b, b.count() results in 3.

Bitset conversions

As demonstrated, bitsets can be initialized with a binary-representing string. It can also be initialized from an existing integer of type unsigned long int. The following program counts in binary from 0 to 31:

#include <bitset> #include <iostream> using namespace std; int main() { for (int i = 0; i < 32; ++i) cout << bitset<4>(i) << endl; return 0; }

The output of this program is:

0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111

Instead of inserting a bitset directly into an output stream, one can convert it into a string or integer.

For a bitset named b: the expression b.to_ulong() returns an unsigned long int containing the bits of the bitset and the expression b.to_string() returns an std::string containing the bits of the bitset represented by the characters '0' for 0 and '1' for 1.