bitset<N>

The bitset container represents an arbitrary sequence of binary 1 and 0 values. Unlike the other STL containers, the bitset is templatized on an integer value. For example, bitset<10> is a set of ten bits, while bitset<128> is a set of 128 bits. Also unlike the other STL container classes, bitsets cannot be resized once created.

bitsets are rather compact data structures and are useful for manipulating bit flags at a high-level. The C++ bitwise operators generalize to bitset, and bitset introduces several high-level means for manipulating bits.

Common bitset<N> Functions
bitset<N>();
Constructs a bitset of size N with all bits set to zero.
bitset<137>(); // A bitset of 137 bits, all set to zero.

Constructs a new bitset of size N, with all bits set to zero.

bitset<N>(unsigned long initialValue);
Constructs a bitset of size N based on the bits set in the parameter.
bitset<32>(myVal); // A bitset of 32 bits initialized to the bits in myVal.

Constructs a new bitset of size N, initialized to the values of the bits in initialValue. For example, if initialValue has binary representation 01000011...01100001, the bitset would be initialized to have this same bit pattern.

If there are more bits in the unsigned long value than in the bitset, then the bitset will be initialized to the lower-order bits in the unsigned long. For example, when working with a bitset<2>, initializing the bitset to the unsigned long value of 0000...1111 would give the bitset the value 11.

If there are more bits in the bitset than in the unsigned long value, then the bitset will initialize its lower-order bits to the value of the unsigned long and its higher-order bits to zero. In other words, it will initialize as many bits as possible using the unsigned long value, filling the rest with zeroes.

bitset<N>& set();
bitset<N>& set(size_t pos, int val = 1);
Sets bits.
myBitset.set(); // All bits set to 1
myBitset.set(4); // Sets bit 4 to 1
myBitset.set(4, 0); // Sets bit 4 to zero.

If no parameters are specified, sets all bits in the bitset to 1. If a single parameter is specified, sets the bit at that index to 1, throwing an out_of_range exception if the index is out of bounds. If two parameters are specified, sets the bit at the specified index to 1 if the second parameter is nonzero, or to 0 otherwise.

set returns a reference to the bitset, so you can chain it in operations such as myBitset.set(0).set(1);

bitset<N>& reset();
bitset<N>& reset(size_t pos);
Clears bits.
myBitset.reset(); // All bits set to 0
myBitset.reset(4); // Sets bit 4 to 0

If no parameters are specified, sets all bits in the bitset to 0. If a single parameter is specified, sets the bit at that index to 0, throwing an out_of_range exception if the index is out of bounds. reset returns a reference to the bitset.

bitset<N>& flip();
bitset<N>& flip(size_t pos);
Flips bits.
myBitset.flip(); // Flips all bits.
myBitset.flip(4); // Flip bit 4.

If no parameters are specified, flips all bits in the bitset. If a single parameter is specified, flips the bit at that index, throwing an out_of_range exception if the index is out of bounds. flip returns a reference to the bitset.

bool any() const;
Returns if any bit is set.
if(myBitset.any()) { /* ... */ } // Some bit is set.

Returns whether any of the bits are set.

bool none() const;
Returns if no bits are set.
if(myBitset.none()) { /* ... */ } // No bits are set.

Returns whether none of the bits are set.

bool test(size_t pos) const;
Returns the value of the specified bit, if possible.
if(myBitset.test(0)) { /* ... */ } // Bit zero is set.

Returns the value of the bit at the specified index, throwing an out_of_range exception if the index is out of bounds.

Bitwise Operators
bitset<N> operator& (const bitset<N> &one, const bitset<N> &two);
bitset<N>& operator&= (const bitset<N> &other);
Bitwise AND.
bitset<4> three = one & two; // three is the bitwise AND of one and two.
myBitset &= other; // Applies bitwise AND to all bits.

Applies the bitwise AND operator to all bits in one bitset based on the bits in another bitset.

bitset<N> operator| (const bitset<N> &one, const bitset<N> &two);
bitset<N>& operator|= (const bitset<N> &other);
Bitwise OR.
bitset<4> three = one | two; // three is the bitwise OR of one and two.
myBitset |= other; // Applies bitwise OR to all bits.

Applies the bitwise OR operator to all bits in one bitset based on the bits in another bitset.

bitset<N> operator^ (const bitset<N> &one, const bitset<N> &two);
bitset<N>& operator^= (const bitset<N> &other);
Bitwise XOR.
bitset<4> three = one ^ two; // three is the bitwise XOR of one and two.
myBitset ^= other; // Applies bitwise XOR to all bits.

Applies the bitwise XOR operator to all bits in one bitset based on the bits in another bitset.

bitset<N> operator << (size_t amount) const;
bitset<N>& operator <<= (size_t amount);
Left bitshift.
bitset<4> two = one << 3; // two has the value of one after bits are left-shifted three positions.
myBitset <<= 4; // Shifts all bits over four positions to the left.

Applies a left-bitshift to the bitset, moving all bits to the left by the specified amount. Low-order bits are reset to zero.

bitset<N> operator >> (size_t amount) const;
bitset<N>& operator >>= (size_t amount);
Right bitshift.
bitset<4> two = one >> 3; // two has the value of one after bits are right-shifted three positions.
myBitset >>= 4; // Shifts all bits over four positions to the right.

Applies a left-bitshift to the bitset, moving all bits to the right by the specified amount. High-order bits are reset to zero.

bitset<N> operator ~ (size_t amount) const;
Bitwise complement.
bitset<4> two = ~one // two has the value of the inverse of one.

Returns a new bitset where all bits are equal to the inverse of the bits in the source.

Uncommon bitset<N> Functions
bitset<N>(const string &str, string::size_type start = 0, string::size_type stop = string::npos);
Constructs a bitset of size N based on a string representation of those bits.
bitset<5>("11001"); // A bitset of 5 bits initialized to 11001.
bitset<5>("xxxx11001xxxx", 4, 5); // Initializes the bitset based on the five characters in the string, starting at char 4.

Constructs a new bitset of size N, initialized to the values set in some substring of the string str. The bitset treats the characters 1 and 0 as their binary equivalents. If the bitset encounters any other characters, it will throw an invalid_argument exception.

If you provide only a string parameter to the bitset constructor, it will use the entire string as its source. Providing a string and and a single integer value will use the substring of string beginning at that offset. If the offset it greater than the length of the string, the bitset constructor will throw an out_of_range exception. If you provide a string and two integer values, the bitset constructor will use the substring of the str parameter given by [pos, pos + len).

If there are more bits in the bitset than in the specified substring, then the low-order bits will be set to the values specified in the string and the high-order bits will be initialized to zero. For example, bitset<8>("1111") will create a bitset holding the values 00001111.

If there are more bits in the specified substring than in the bitset, then the bitset will initialize itself to the first N values specified in the string, ignoring the rest. This behavior is not the same behavior exhibited with the constructor taking an unsigned long. For example, bitset<4>("11110000") initializes the bitset to 1111, not 0000.

unsigned long to_ulong() const;
Converts the bitset to an unsigned long, if possible.
unsigned long myLong = myBitset.to_ulong(); // Get an unsigned long value.

Returns an unsigned long with the same bit sequence as the bitset. If the value contained in the bitset cannot be represented as an unsigned long, this function throws an overflow_error.

string to_string() const;
Converts the bitset to a string.
cout << myBitset.to_string() << endl; // Print string representation.

Returns a string representation of the bitset, with the lowest bit as the rightmost character.

bool operator[] (size_t pos) const;
bitset<N>::reference operator[] (size_t pos);
Returns the bit at the specified index
cout << myBitset[0] << endl; // Prints bit zero.
myBitset[137] = false; // Clears bit 137
myBitset[42].flip(); // Flips bit 42

Returns the bit at the specified position. The strange syntax bitset<N>::reference for the non-const version means that the object handed back acts like a bool, though it technically is not. In addition to all of the functions of a bool, the returned object may have the flip method invoked on it, which toggles the state of the bit. This is an advanced technique known as proxy classes and if you are interested in how it works, I'd be more than happy to discuss it.

size_t size() const;
Returns the number of bits in the bitset.
if(myBitset.size() == 3) { /* ... */ } // Exactly three bits in the bitset

Returns the number of bits in the bitset. This is equal to the template parameter.

size_t count() const;
Returns the number of bits set.
if(myBitset.count() == 3) { /* ... */ } // Exactly three bits set.

Returns the number of bits with value 1 in the bitset.