![]() |
VOOZH | about |
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
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:
Example 1:
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:
Example 2:
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:
Reference: https://www.boost.org/doc/libs/1_36_0/libs/dynamic_bitset/dynamic_bitset.html