Bitset is a container for dealing with data at the bit level

View versions (1)

Definition

The class bitset is defined in the standard header <bitset>.

#include <bitset>
namespace std {
       template <size_t Bits>
       class bitset;
}

Description

Bitset is a class that describes objects that can store a sequence consisting of a fixed number of bits. A bit is set if its value is 1, reset if its value is 0.

A simple example of creating a bitset is:

std::bitset <8> bS; // creates a bitset holding 8 bits all initialized to 0

Bitset is very similar to vector<bool> (also known as http://codecogs.izyba.com/reference/computing/containers/sequences/bit_vector.php"bit_vector" ): it contains a collection of bits, and provides constant-time access to each bit. Unlike the other STL container classes, bitsets cannot be resized once created.

Performance

Bitsets are 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.

Errors and Exceptions

Bitset constructors and member functions may throw the following exceptions:

  • invalid_argument (when a parameter is invalid)
  • out_of_range (when a parameter is outside the valid range)
  • overflow_error (when an arithmetic overflow is encountered)

Bitset Operations

Create, Copy, and Destroy Operations

For bitsets, some special constructors are defined. There is no special copy constructor, assignment operator, and destructor defined. Thus, bitsets are assigned and copied with the default operations that copy bitwise.

OperationEffect
bitset< N >()Creates a bitset of size N with all bits set to zero
bitset< N >(unsigned long initialValue)Creates a bitset of size N based on the bits set in the parameter
bitset< N >(const stringstr, string::size_type start = 0, string::size_type stop = string::npos)Creates a bitset of size N based on a string representation of those bits

Nonmanipulating Operations

OperationEffect
size_t size() constReturns the number of bits
size_t count() constReturns the number of set bits (bits with value 1)
bool any() constReturns if any bit is set
bool none() constReturns if no bit is set
bool test (size_t pos) constReturns if the bit at position pos is set (throws out_of_range if pos > size())
bool operator==(const bitset<bits>bits) constReturns if all bits of *this and bits have the same value
bool operator!=(const bitset<bits>bits) constReturns if any bits of *this and bits have a different value

Manipulating Operations

OperationEffect
bitset< N >set()Sets all bits to true, returns the modified bitset
bitset< N >set(size_t pos)Sets the bit at position pos to true, returns the modified bitset (throws out_of_range if pos > size())
bitset< N >set(size_t pos, int val)Sets the bit at position pos according to val, returns the modified bitset. If val is equal to 0, the bit is set to false; any other value sets the bit to true (throws out_of _range if idx > size())
bitset< N >reset()Resets all bits to false (assigns 0 to all bits) and returns the modified bitset
bitset< N >reset(size_t pos)Resets the bit at position pos to false, returns the modified bitset (throws out_of_range if pos > size())
bitset< N >flip()Toggles all bits (sets unset bits and vice versa), returns the modified bitset
bitset< N >flip(size_t pos)Toggles the bit at position pos, returns the modified bitset (throws out_of_range if pos > size())
bitset< N >operator^= (const bitset< N >bits)The bitwise exclusive-or operator; toggles the value of all bits that are set in bits and leaves all other bits unchanged; returns the modified bitset
bitset< N >operator|= (const bitset< N >bits)The bitwise or operator; sets all bits that are set in bits and leaves all other bits unchanged; returns the modified bitset
bitset< N >\b{operator=} (const bitset< N >bits)The bitwise and operator; resets all bits that are not set in bits and leaves all other bits unchanged; returns the modified bitset
bitset< N >operator<<= (size_t num)Shifts all bits by num positions to the left; returns the modified bitset; the first num bits are set to false
bitset< N >operator>>= (size_t num)Shifts all bits by num positions to the right; returns the modified bitset; the last num bits are set to false

Access with Operator [ ]

OperationEffect
  • bool operator[](size_t pos) const
  • reference operator[](size_t pos)
The member function returns an object of class reference, which designates the bit at position pos, if the object can be modified. Otherwise, it returns the value of the bit at position pos in the bit sequence. If that position is invalid, the behavior is undefined

Creating New Modified Bitsets

OperationEffect
bitset< N > operator~() constReturns a new bitset that has all bits toggled with respect to *this
bitset< N > operator<< (size_t num) constReturns a new bitset that has all bits shifted to the left by num position
bitset< N > operator>> (size_t num) constReturns a new bitset that has all bits shifted to the right by num position
bitset< N > \b{operator} (const bitset< N >b1, const bitset <N>b2)Returns the bitwise computing of operator and of b1 and b2; returns a new bitset that has only those bits set in b1 and in b2
bitset< N > operator| (const bitset< N >b1, const bitset <N>b2)Returns the bitwise computing of operator or of b1 and b2; returns a new bitset that has only those bits set in b1 or in b2
bitset< N > operator^ (const bitset< N >b1, const bitset <N>b2)Returns the bitwise computing of operator exclusive-or of b1 and b2; returns a new bitset that has only those bits set in b1 and not set in b2 or vice versa

Operations for Type Conversions

OperationEffect
unsigned long to_ulong() constReturns the integral value that the bits of the bitset represent (throws overflow_error if the integral value can't be represented by type unsigned long
string to_string() constConverts the bitset to a string; returns a string that contains the value of the bitset as a binary representation written with characters '0' for unset bits and '1' for set bits

Input/Output Operations

OperationEffect
istreamoperator>> (istreamstrm, bitset<bits>bits)Reads into bits a bitset as a character sequence of characters '0' and '1' until: at most, bits characters are read or end-of-file occurs in strm or the next character is neither '0' nor '1'.If the number of bits read is less than the number of bits in the bitset, the bitset is filled with leading zeros
ostreamoperator<< (ostreamstrm, const bitset<bits>bits)Writes bits converted into a string that contains the binary representation (thus, as a sequence of '0' and '1')

References

Example 1
Problem

This example of program illustrates how to use operator& when working with bitsets.

Workings
#include <iostream>
#include <bitset>
#include <string>

using namespace std;

int main()
{
  bitset<4> b1 ( string("0101") );
  bitset<4> b2 ( string("0011") ); 

  // b3 is a new bitset that has only those bits set in b1 and in b2
  bitset<4> b3 = b1 & b2; 
  cout << "bitset 1: " << b1 << endl;
  cout << "bitset 2: " << b2 << endl;
  cout << "bitset 3: " << b3 << endl; 

  return 0;
}
Solution

Output:

bitset 1: 0101
bitset 2: 0011
bitset 3: 0001

References