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.
bitset<N> Functionsbitset of size N with all bits set to zero.Constructs a new bitset of size N, with all bits set to zero.
bitset of size N based on the bits set in the parameter.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.
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);
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.
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.
Returns whether any of the bits are set.
Returns whether none of the bits are set.
Returns the value of the bit at the specified index, throwing an out_of_range exception if the index is out of bounds.
Applies the bitwise AND operator to all bits in one bitset based on the bits in another bitset.
Applies the bitwise OR operator to all bits in one bitset based on the bits in another bitset.
Applies the bitwise XOR operator to all bits in one bitset based on the bits in another bitset.
Applies a left-bitshift to the bitset, moving all bits to the left by the specified amount. Low-order bits are reset to zero.
Applies a left-bitshift to the bitset, moving all bits to the right by the specified amount. High-order bits are reset to zero.
Returns a new bitset where all bits are equal to the inverse of the bits in the source.
bitset<N> Functionsbitset of size N based on a string representation of those bits.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.
bitset to an unsigned long, if possible.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.
bitset to a string.Returns a string representation of the bitset, with the lowest bit as the rightmost character.
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.
bitset.Returns the number of bits in the bitset. This is equal to the template parameter.
Returns the number of bits with value 1 in the bitset.