VOOZH about

URL: https://www.geeksforgeeks.org/cpp/c-boostdynamic_bitset-class-with-examples/

⇱ C++ boost::dynamic_bitset Class with Examples - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

C++ boost::dynamic_bitset Class with Examples

Last Updated : 12 Jul, 2025

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:


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: 


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

Comment
Article Tags:
Article Tags: