I have forgotten
my Password

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingStlContainers

Bitset

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

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

Operation Effect
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 string& str, 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

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

Manipulating Operations

Operation Effect
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 >& 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 [ ]
Operation Effect
  • 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
Operation Effect
bitset< N > operator~() const Returns a new bitset that has all bits toggled with respect to *this
bitset< N > operator<< (size_t num) const Returns a new bitset that has all bits shifted to the left by num position
bitset< N > operator>> (size_t num) const Returns a new bitset that has all bits shifted to the right by num position
bitset< N > 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
Operation Effect
unsigned long to_ulong() const Returns 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() const Converts 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
Operation Effect
istream& operator>> (istream& strm, 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
ostream& operator<< (ostream& strm, 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:

Example - bitsets operator &
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