Open In App

C++ boost::dynamic_bitset Class with Examples

Last Updated : 20 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The boost has more than 150 libraries in it, where a couple of most frequently used libraries were already included in C++ standard library. The dynamic_bitset is a powerful library used for bit manipulation. The dynamic_bitset class is used to represent a set of bits in either 0(reset) or 1(set) form. dynamic_bitset is an improvement over bitset (std::bitset and boost::bitset) which allocates any required length of bits at runtime, contrary to bitset whose bit’s length has to be determined at compile time.
The dynamic_bitset if found under the boost header boost/dynamic_bitset.hpp.

Syntax:  

boost::dynamic_bitset <uint8_t> B (N, num);

The parameters of the constructors are  

  • N which signifies the required number of bits in the set.
  • num signifies any integral value whose bits will be stored.
  • uint8_t signifies the block size (8 here, it can also be empty if we don’t need to specify block size).

Each individual bits of the dynamic_bitset can be accessed similar to bitset indexing operator []. 
Please note that bit representation of a number B in dynamic_bitset of length n is represented as:  

B[n-1] B[n-2] ... B[1] B[0]

In other words, the indexing in dynamic_bitset works in reverse order (similar to bitset). Each bit in dynamic_bitset takes exactly 1-bit space because of its optimization thus making operations faster than boolean vector.
Member Functions: 
The basic member functions that can be performed on dynamic_bitset are listed below: 

  • set(): It assigns every bit with 1
  • reset(): It assigns nth bit with 0 if the n is passed in argument, else it clears the entire bitset object.
  • flip(): It inverts any given bit i.e. toggle 0 to 1 or vice-versa
  • size(): It returns the size of the dynamic_bitset object
  • resize(): It is used to increase or decrease the size of the object
  • push_back(): It increases the size of dynamic_bitset object by one and pushes the value at MSB
  • pop_back(): It removes one bit from MSB
  • num_blocks(): It returns the number of blocks in bitset
  • append: It appends the bits at the most-significant bit. This increases the size of the bitset by bits_per_block. For i in the range [0, bits_per_block), the bit at position (size + i) is set to ((value >> i) & 1)
  • empty(): It returns true if the length of dynamic_bitset is 0, else it returns false.
  • count(): It returns the number of set bits in dynamic_bitset.
  • all(): It tests if all of the bits in dynamic_bitset are set, if they are it returns true else false.
  • any(): It tests if atleast one of the bit in dynamic_bitset is set, if they are it returns true else false.
  • none(): It tests if none of the bits in dynamic_bitset are set, if none of them are set then it returns true else false.
  • test(): It tests if the ith bit is set or not. If set it returns true else false.

Example 1:

CPP




#include <boost/dynamic_bitset.hpp>
#include <iostream>
 
using namespace std;
 
int main(int argc, char* argv[])
{
    int bit_size = 8;
 
    // B1 is initialized with size 0
    // with all bits 0
    boost::dynamic_bitset<> B1;
 
    // B2 is initialized with size
    // bit_size with all bits 0
    boost::dynamic_bitset<> B2(bit_size);
 
    // B3 is initialized with size
    // bit_size and value 14
    boost::dynamic_bitset<> B3(bit_size, 14);
 
    // B4 is initialized with size
    // bit_size, value 14 and
    // block_size of 8 bits
    boost::dynamic_bitset<uint8_t> B4(16, 84);
 
    // Empty
    cout << "Content of B1 is: "
         << B1 << endl;
 
    // 00000000
    cout << "Content of B2 is: "
         << B2 << endl;
    cout << "Binary representation of 14 in 8 bit: "
         << B3 << endl;
    cout << "Content of B4 is: "
         << B4 << endl
         << endl;
 
    // Setting 1 st of B2 to 1
    cout << "Content of B2 before set(): "
         << B2 << endl;
 
    B2.set(0);
    cout << "Content of B2 after set(0): "
         << B2 << endl;
 
    // Setting every bits of B22 to 1
    B2.set();
    cout << "Content of B2 after set(): "
         << B2 << endl;
 
    // Resetting 2nd bit to 0
    B2.reset(1);
    cout << "After resetting 2nd bit of B2: "
         << B2<< endl;
 
    // Resetting every bit to 0
    B2.reset();
    cout << "After resetting every bit of B2: "
         << B2 << endl;
 
    // Flipping first bit of B3
    cout << "Content of B3 before flip(): "
         << B3 << endl;
 
    B3.flip(0);
    cout << "Content of B3 after flip(0): "
         << B3 << endl;
 
    // Flipping every bits of B3
    B3.flip();
    cout << "Content of B3 after flip():  "
         << B3 << endl
         << endl;
 
    // Size of B1, B2, B3, B4
    cout << "Size of B1 is: "
         << B1.size() << endl;
    cout << "Size of B2 is: "
         << B2.size() << endl;
    cout << "Size of B3 is: "
         << B3.size() << endl;
    cout << "Size of B4 is: "
         << B4.size() << endl
         << endl;
 
    // Resizing B1 to size 4,
    // default bit-value 0
    B1.resize(4);
    cout << "B1 after increasing size to 4 bits: "
         << B1 << endl;
 
    // Resizing B1 to size 8, bit-value 1.
    // If num_bits > size() then the bits
    // in the range [0, size()) remain the same,
    // and the bits in [size(), num_bits)
    // are all set to value (here 1).
    B1.resize(8, 1);
    cout << "B1 after increasing size to 8 bits: "
         << B1 << endl;
 
    // Resizing B1 to size 1 i.e.
    // slicing [1, B1.size()-1)
    B1.resize(1);
    cout << "B1 after decreasing size to 1 bit:  "
         << B1 << endl
         << endl;
 
    // Pushing a set bit at MSB in B1
    B1.push_back(1);
    cout << "B1 after push(1) operation:   "
         << B1 << endl;
 
    // Pushing a reset bit at MSB in B1
    B1.push_back(0);
    cout << "B1 after push(0) operation : "
         << B1 << endl
         << endl;
 
    // Popping 1 bit from MSB in B1
    cout << "B1 before pop operation: "
         << B1 << endl;
 
    B1.pop_back();
    cout << "B1 after pop operation:   "
         << B1 << endl
         << endl;
 
    // Number of blocks = number of bits / block size
    cout << "Number of blocks in B4: "
         << B4.num_blocks() << endl
         << endl;
 
    // Checking if any bitset is empty
    cout << "B1 is "
         << (B1.empty() ? "empty" : "not empty")
         << endl;
    cout << "B2 is "
         << (B2.empty() ? "empty" : "not empty")
         << endl;
 
    // Resizing B3 to 0
    B3.resize(0);
    cout << "B3 is "
         << (B3.empty() ? "empty" : "not empty")
         << endl
         << endl;
 
    // Counting number of set bits in B4
    cout << "Content of B4 is: " << B4 << endl;
    cout << "Number of set bits in it are: "
         << B4.count() << endl
         << endl;
 
    // Checking if all of the bits of B2 is set
    B2.set(); // B2 => 11111111
 
    cout << "All bits in B2 are "
         << (B2.all() ? "set" : "not set") << endl;
 
    B2.reset(2); // B2 => 11111011
    cout << "All bits in B2 are "
         << (B2.all() ? "set" : "not set")
         << endl
         << endl;
 
    // Checking if any of the bits of B2 is set
    cout << (B2.any() ? "Atleast one" : "No")
         << " bit in B2 is set " << endl;
    B2.reset(); // B2 => 00000000
 
    cout << (B2.any() ? "Atleast one" : "No")
         << " bit in B2 is set " << endl
         << endl;
 
    // Checking if none of the bits of B2 are set
    // B2 => 00000000
    if (B2.none())
        cout << "None of the bits in B2 is set";
    else
        cout << "Atleast one bit in B2 is set";
    cout << endl
         << endl;
 
    // Testing if 1st bit of B1 is set or not
    cout << "Content of B1 is: " << B1 << endl;
    if (B1.test(1))
        cout << "B1[1] is set";
    else
        cout << "B1[1] is reset";
    return 0;
}


Output: 

Content of B1 is: 
Content of B2 is: 00000000
Binary representation of 14 in 8 bit: 00001110
Content of B4 is: 0000000001010100

Content of B2 before set(): 00000000
Content of B2 after set(0): 00000001
Content of B2 after set(): 11111111
After resetting 2nd bit of B2: 11111101
After resetting every bit of B2: 00000000
Content of B3 before flip(): 00001110
Content of B3 after flip(0): 00001111
Content of B3 after flip():  11110000

Size of B1 is: 0
Size of B2 is: 8
Size of B3 is: 8
Size of B4 is: 16

B1 after increasing size to 4 bits: 0000
B1 after increasing size to 8 bits: 11110000
B1 after decreasing size to 1 bit:  0

B1 after push(1) operation:   10
B1 after push(0) operation : 010

B1 before pop operation: 010
B1 after pop operation:   10

Number of blocks in B4: 2

B1 is not empty
B2 is not empty
B3 is empty

Content of B4 is: 0000000001010100
Number of set bits in it are: 3

All bits in B2 are set
All bits in B2 are not set

Atleast one bit in B2 is set 
No bit in B2 is set 

None of the bits in B2 is set

Content of B1 is: 10
B1[1] is set

Operators: 
Some of the operators that can be helpful for bit manipulation:

  • operator[]: It returns a reference of nth bit.
  • operator&=(): It performs bitwise-AND with current bitset object and bitset object passed in argument. Eg. B1.operator&=(B2); bitwise-AND of B1 and B2 i.e. B1 & B2 (B1 and B2 must have same number of bits).
  • operator|=(): It performs bitwise-OR with current bitset object and bitset object passed in argument. Eg. B1.operator|=(B2); bitwise-OR of B1 and B2 i.e. B1 | B2 (B1 and B2 must have same number of bits).
  • operator^=(): It performs bitwise-XOR with current bitset object and bitset object passed in argument. Eg. B1.operator^=(B2); bitwise-XOR of B1 and B2 i.e. B1 ^ B2 (B1 and B2 must have same number of bits).
  • operator-=(): It performs set difference with current bitset object and bitset object passed in argument. Eg. B1.operator-=(B2); set difference of B1 and B2 i.e. B1 – B2 (B1 and B2 must have same number of bits).
  • operator=(): It assigns current bitset object with object passed in parameter.
  • operator==(): It validates if current bitset object is exactly equal to that of passed as parameter.
  • operator!=(): It validates if current bitset object is not equal to that of passed as parameter.
  • operator<(): It returns true if current bitset is lexicographically less than the bitset object passed as parameter, else it returns false.
  • operator>(): It returns true if current bitset is lexicographically greater than the bitset object passed as parameter, else it returns false.
  • operator<=(): It returns true if current bitset is lexicographically less than or equal to the bitset object passed as parameter, else it returns false.
  • operator>=(): It returns true if current bitset is lexicographically greater than or equal to the bitset object passed as parameter, else it returns false.
  • operator~(): It creates a copy of the current bitset with all of its bits flipped.
  • operator<<(): It creates a copy of the current bitset object which is shifted to the left by n bits.
  • operator>>(): It creates a copy of the current bitset object which is shifted to the right by n bits.
  • operator<<=(): It shifts the current bitset object to left by n bits.
  • operator>>=(): It shifts the current bitset object to right by n bits.

Example 2: 

CPP




#include <boost/dynamic_bitset.hpp>
#include <iostream>
using namespace std;
 
int main(int argc, char* argv[])
{
    int n_bits = 8;
 
    boost::dynamic_bitset<> B1(n_bits, 123);
    boost::dynamic_bitset<> B2(n_bits, 206);
    boost::dynamic_bitset<> temp(4, 3);
 
    cout << "Binary representation of 123: "
         << B1 << endl;
    cout << "Binary representation of 206: "
         << B2 << endl
         << endl;
 
    // Operator[] is used to access an individual index
    // It is a reference of the nth bit. It can be used for
    // assignment of a boolean value at nth bit
    cout << "4th bit of B1 consist: "
         << B1[3] << endl;
    B1[3] = 0;
    cout << "Assigning 0 to 4th bit of B1: "
         << B1 << endl
         << endl;
 
    // Operator= assigns on current bitset object
    cout << "temp before assignment: "
         << temp << endl;
 
    temp.operator=(B1);
    cout << "temp after assignment with B1: "
         << temp << endl
         << endl;
 
    // Operator&= performs bitwise-AND
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
 
    temp.operator=(B1);
    temp.operator&=(B2);
    cout << "B1 AND B2 is : & "
         << temp << endl
         << endl;
 
    // Operator|= performs bitwise-OR
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
 
    temp.operator=(B1);
    temp.operator|=(B2);
    cout << "B1 OR B2 is  : | "
         << temp << endl
         << endl;
 
    // Operator^= performs bitwise-XOR
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
 
    temp.operator=(B1);
    temp.operator^=(B2);
    cout << "B1 XOR B2 is : ^ "
         << temp << endl
         << endl;
 
    // Operator-= performs set difference
    cout << "B1 consist of: "
         << B1 << endl;
    cout << "B2 consist of: "
         << B2 << endl;
 
    temp.operator=(B1);
    temp.operator-=(B2);
    cout << "Set differ is:   "
         << temp << endl
         << endl;
 
    // Operator== checks if bitset object is equal to
    // another one, bit length has to be same for true
    cout << "dynamic_bitset B1 and B2 are "
         << (operator==(B1, B2) ? "equal" : "not equal")
         << endl;
 
    boost::dynamic_bitset<> B3(2, 0), B4(3, 0);
    cout << "Content of B3: " << B3 << endl
         << "Content of B4: " << B4 << endl
         << "dynamic_bitset B3 and B4 are "
         << (operator==(B3, B4) ? "equal" : "not equal")
         << endl;
 
    B3.operator=(B4);
    cout << "dynamic_bitset B3 and B4 are: "
         << (operator==(B3, B4) ? "equal" : "not equal")
         << endl
         << endl;
 
    // Operator!= checks if bitset object is unequal
    cout << "dynamic_bitset B1 and B2 are ";
    cout << (operator!=(B1, B2) ? "not equal" : "equal")
         << endl
         << endl;
 
    // Operator< checks if first bitset object is
    // lexicographically less than second one
    cout << "B1 consist of: " << B1 << endl;
    cout << "B2 consist of: " << B2 << endl;
 
    if (operator<(B1, B2))
        cout << "B1 is lexicographically less than B2";
    else
        cout << "B2 is lexicographically greater than B2";
    cout << endl
         << endl;
 
    // Operator> checks if first bitset object is
    // lexicographically greater than second one
    cout << "B1 consist of: " << B1 << endl;
    boost::dynamic_bitset<> B5(8, 0);
    cout << "B5 consist of: " << B5 << endl;
 
    if (operator>(B1, B5))
        cout << "B1 is lexicographically greater than B5";
    else
        cout << "B1 is lexicographically less than B5";
    cout << endl
         << endl;
 
    // Operator<= checks if first bitset object is
    // lexicographically less than or equal to second one
    cout << "B3 is lexicographically ";
    if (operator<=(B3, B3))
        cout << "less than or equal to B3"
             << endl
             << endl;
    else
        cout << "greater than B3"
             << endl
             << endl;
 
    // Operator>=
    cout << "B5 consist of: " << B5 << endl;
    cout << "B2 consist of: " << B2 << endl;
    cout << "B5 is lexicographically ";
    if (operator>=(B5, B2))
        cout << "greater than or equal to B2";
    else
        cout << "less than B2";
    cout << endl
         << endl;
 
    // Operator~ creates a copy of flipped bitset object
    cout << "Value of dynamic_bitset B4 : " << B4 << endl;
    cout << "Creating flipped copy of B4: ";
    cout << B4.operator~() << endl
         << endl;
 
    // Operator<< creates a copy of current bitset object
    // which is shifted to left by n times
    cout << "Value of dynamic_bitset B2: " << B2 << endl;
    cout << "Copy of B2 left shift 3 times is  : ";
    cout << B2.operator<<(3) << endl;
 
    // Operator>> creates a copy of current bitset object
    // which is shifted to right by n times value of B2
    // is not changed, the copy is displayed
    cout << "Copy of B2 right shift 1 time is  : ";
    cout << B2.operator>>(1) << endl
         << endl;
 
    // Operator<<= shifts the current bitset object
    // n times to left
    cout << "Value of dynamic_bitset B2: "
         << B2 << endl;
    cout << "B2 left shift 3 times is  : ";
    cout << B2.operator<<=(2) << endl;
 
    // Operator>>= shifts current bitset object
    // n times value to right
    cout << "B2 right shift 1 time is  : ";
    cout << B2.operator>>=(3);
 
    return 0;
}


Output: 

Binary representation of 123: 01111011
Binary representation of 206: 11001110

4th bit of B1 consist: 1
Assigning 0 to 4th bit of B1: 01110011

temp before assignment: 0011
temp after assignment with B1: 01110011

B1 consist of: 01110011
B2 consist of: 11001110
B1 AND B2 is : & 01000010

B1 consist of: 01110011
B2 consist of: 11001110
B1 OR B2 is  : | 11111111

B1 consist of: 01110011
B2 consist of: 11001110
B1 XOR B2 is : ^ 10111101

B1 consist of: 01110011
B2 consist of: 11001110
Set differ is:   00110001

dynamic_bitset B1 and B2 are not equal
Content of B3: 00
Content of B4: 000
dynamic_bitset B3 and B4 are not equal
dynamic_bitset B3 and B4 are: equal

dynamic_bitset B1 and B2 are not equal

B1 consist of: 01110011
B2 consist of: 11001110
B1 is lexicographically less than B2

B1 consist of: 01110011
B5 consist of: 00000000
B1 is lexicographically greater than B5

B3 is lexicographically less than or equal to B3

B5 consist of: 00000000
B2 consist of: 11001110
B5 is lexicographically less than B2

Value of dynamic_bitset B4 : 000
Creating flipped copy of B4: 111

Value of dynamic_bitset B2: 11001110
Copy of B2 left shift 3 times is  : 01110000
Copy of B2 right shift 1 time is  : 01100111

Value of dynamic_bitset B2: 11001110
B2 left shift 3 times is  : 00111000
B2 right shift 1 time is  : 00000111

 

Applications:

  • dynamic_bitset can be effectively used to represent a subset of a finite set. Each bit represents whether an element of the finite set is in the subset or not.
  • Sieve of Eratosthenes for finding all prime numbers below an integer N.

Reference: https://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads