//Implementable on C++ 11/14/17
Boost Dynamic Bitset
Description:
boost::dynamic_bitset is just like std::bitset but, there is a difference in execution. The number of bits for std::bitset must be specified at compile-time, whereas that for boost::dynamic_bitset is specified at run time. boost::dynamic_bitset also provides more functionality than std::bitset. A dynamic bitset is an array of boolean values but each boolean is not stored separately instead of that dynamic bitset optimizes the space so that each value takes 1-bit space only. It is also faster to do some operations on dynamic bitset than on arrays and vectors because of the fact that they store information in a compressed manner.
Header File Used:
#include<boost/dynamic_bitset.hpp>
Member Functions:
>all( ):- is used to test whether all bits of the dynamic bitset object are set or not and returns true if all bits are set otherwise it returns false.
>any( ):- is used to check whether atleast one bit is set or not of the dynamic bitset object and returns true if atleast one bit is set otherwise false.
>count( ):- is used to count the number of set bits from the dynamic bitset object and returns the count.
>flip( ):- is used to toggle all the bits of dynamic bitset object, basically inverts all the bits separately. It is also possible to flip just one bit if the position is specified.
>none( ):- checks whether all bits are unset or not and if they are then returns true else returns false.
>find_first( ):- returns the position of the first set bit from least significant bit to most significant bit of the dynamic bitset object.
>find_next( ):- returns the position of the next set bit from the position that is mentioned in the argument.
>push_back( ):- adds another bit which becomes the most significant bit. The bit can be set or unset depending on the argument.
>pop_back( ):- removes the most significant bit.
>set( ):- if there is no argument then sets all bits, otherwise sets the bit according to the argument which represents the position of the bit starting from 0.
>reset( ):- resets all bits if no argument is passed, otherwise resets the bit mentioned in the argument which is the position of the bit.
>resize( ):- it is used to either increase or decrease the number of bits, basically used to resize the dynamic bitset object.
>size( ):- it returns the size of the dynamic bitset object.
>test( ):- it is used to check whether the Nth bit is set or not and returns true if it is set otherwise false.
Program to demonstrate the use of member functions:
#include <iostream>
#include<boost/dynamic_bitset.hpp>
using namespace std;
using namespace boost;
int main()
{
int z;
dynamic_bitset<>bb{ 4,15 };
dynamic_bitset<>bq{ 4, 10 };
dynamic_bitset<>bf{ 3, 0 };
dynamic_bitset<>bg{ 3,4 };
if (bb.all())
{
cout << "all bits are set in bb" << endl;
}
else
{
cout << "all bits are not set" << endl;
}
if (bq.any())
{
cout << bq.count()<<" is the number of set bit in bq" << endl;
}
if (bf.none())
{
cout << "all bits are unset in bf" << endl;
}
cout << "before using flip function bq is" << endl;
cout << bq << endl;
cout << "after using flip function bq is" << endl;
cout << bq.flip() << endl;
cout << "the first set bit from right side of bg is "<<bg.find_first()<<endl;
cout << "Enter a position from where the first set bit of bg will be" << endl;
cin >> z;
cout << bg.find_next(z) << endl;
cout << "bg before adding a bit "<<bg<<" and its size is "<<bg.size() << endl;
bg.push_back(true);
cout << "bg after adding a bit "<<bg <<" and now its size is "<<bg.size()<< endl;
cout << "bf before setting its 0 position bit "<<bf << endl;
if (!bf[0])
{
bf.set(0);
}
cout <<"bf after setting its 0 position bit "<< bf << endl;
cout << "before resizing bf "<<bf<< endl;
bf.resize(5, true);
cout << "after resizing bf will be " << bf << endl;
cout << "enter 2 values in decimal format" << endl;
int a, b;
std::cin >> a >> b;
cout << "the binary numbers between " << a << " and " << b << " are " << endl;
for (int i = a;i <= b;i++)
{
std::cout << boost::dynamic_bitset<>(8,i) << std::endl;
}
return 0;
}
Output:
all bits are set in bb
2 is the number of set bit in bq
all bits are unset in bf
before using flip function bq is
1010
after using flip function bq is
0101
the first set bit from right side of bg is 2
Enter a position from where the first set bit of bg will be
1
2
bg before adding a bit 100 and its size is 3
bg after adding a bit 1100 and now its size is 4
bf before setting its 0 position bit 000
bf after setting its 0 position bit 001
before resizing bf 001
after resizing bf will be11001
enter 2 values in decimal format
7
15
the binary numbers between 7 and 15 are
00000111
00001000
00001001
00001010
00001011
00001100
00001101
00001110
00001111
>operator&=:- performs bitwise AND operation on current bitset objects. E.g: a.operator&=(b); //where a and b are dynamic bitset object.
>operator-=:- performs bitwise subtraction on current bitset object. E.g: a.operator-=(b); //a=a-b bitwise.
>operator<<=:- performs bitwise left shift operation on current bitset object. E.g: a.operator<<=(3); //shifts 3 bits left.
>operator<<:- works the same as operator<<= but does not manipulate the dynamic bitset object, it must be stored in a variable to get the resultant output. E.g: auto result=a.operator<<(2);//shifts 2 bits left.
>operator>>=:- performs bitwise right shift operation on current bitset object. E.g: a.operator>>=(3); //shifts 3 bits right.
>operator>>:- works the same as operator>>= but does not manipulate the dynamic bitset object, it must be stored in a variable to get the resultant output. E.g: auto result=a.operator>>(2);//shifts 2 bits right.
>operator=:- is used as an assignment operator on the current bitset objects. E.g: a.operator=(b);//assigns b to a bitwise.
>operator[ ]:- returns the value of the bit at the required position. E.g: a.operator[ ](1); //returns the value at position 1
>operator|=:- performs bitwise OR operation on current bitset objects. E.g: a.operator|=(b);
>operator^=:- performs bitwise XOR operation on current bitset objects. E.g: a.operator^=(b);
>operator~:- works the same as NOT operator but does not manipulate the dynamic bitset object, it must be stored in a variable to get the resultant output. E.g: auto result=b.operator~( );
Program to demonstrate the use of operators:
#include <iostream>
#include<boost/dynamic_bitset.hpp>
using namespace std;
using namespace boost;
int main()
{
dynamic_bitset<>bb{ 4,13 };
dynamic_bitset<>bq{ 4,10 };
int a;
cout << "before &= operation bb is " << bb<<" and bq is "<<bq << endl;
bb.operator&=(bq);
cout << "after &= operation bb is " << bb <<" and bq is " << bq << endl;
cout << "before -= operation bb is " << bb << " and bq is " << bq << endl;
bb.operator-=(bq);
cout << "after -= operation bb is " << bb << " and bq is " << bq << endl;
cout << "before = operation bb is " << bb << " and bq is " << bq << endl;
bb.operator=(bq);
cout << "after = operation bb is " << bb << " and bq is " << bq << endl;
cout << "before <<= operation bb is " << bb << endl;
bb.operator<<=(2);
cout << "after <<= operation bb is " << bb << endl;
cout << "before ^= operation bb is " << bb << " and bq is " << bq << endl;
bb.operator^=(bq);
cout << "after ^= operation bb is " << bb << " and bq is " << bq << endl;
cout << "before |= operation bb is " << bb << " and bq is " << bq << endl;
bb.operator|=(bq);
cout << "after |= operation bb is " << bb << " and bq is " << bq << endl;
cout << "enter a position in bq to know its bit value" << endl;
cin >> a;
cout << bb.operator[](a) << endl;
auto result = bb.operator~();
cout << "bb after NOT operation " << result << endl;
return 0;
}
Output:
before &= operation bb is 1101 and bq is 1010
after &= operation bb is 1000 and bq is 1010
before -= operation bb is 1000 and bq is 1010
after -= operation bb is 0000 and bq is 1010
before = operation bb is 0000 and bq is 1010
after = operation bb is 1010 and bq is 1010
before <<= operation bb is 1010
after <<= operation bb is 1000
before ^= operation bb is 1000 and bq is 1010
after ^= operation bb is 0010 and bq is 1010
before |= operation bb is 0010 and bq is 1010
after |= operation bb is 1010 and bq is 1010
enter a position in bq to know its bit value
1
1
bb after NOT operation 0101
Comments