std::bitset Member Functions
std::bitset class contains some useful member functions to work on the bitset objects. Here is the list of some member functions of std::bitset:
Function Name | Function Description |
---|---|
Set the bit value at the given index to 1. | |
reset() | Set the bit value at a given index to 0. |
Flip the bit value at the given index. | |
Count the number of set bits. | |
test() | Returns the boolean value at the given index. |
Checks if any bit is set. | |
Checks if none bit is set. | |
Check if all bit is set. | |
size() | Returns the size of the bitset. |
to_string() | Converts bitset to std::string. |
to_ulong() | Converts bitset to unsigned long. |
to_ullong() | Converts bitset to unsigned long long. |
Example:
// C++ program to demonstrate the
// use of std::bitset member
// functions
#include <bitset>
#include <iostream>
using namespace std;
int main()
{
// declaring index variable
int index = 0;
// declaring few bitset objects
bitset<4> allSet("1111"), allUnset;
cout << "any() value: " << boolalpha << allSet.any()
<< endl;
cout << "all() value: " << allSet.all() << endl;
cout << "none() value: " << allSet.none() << endl;
cout << "test() at index 0: " << noboolalpha
<< allSet.test(index) << endl;
cout << "size() value: " << allSet.size() << endl;
cout << "Value of allUnset on before using set(): "
<< allUnset << endl;
allUnset.set(index);
cout << "Value of allUnset on after using set(): "
<< allUnset << endl;
cout << "Value of allSet on before using reset(): "
<< allSet << endl;
allSet.reset(index);
cout << "Value of allSet on after using reset(): "
<< allSet << endl;
// declaring an empty string
string bitString;
// using to_string() method to assign value to empty
// string
bitString = allSet.to_string();
cout << "bitString: " << bitString << endl;
cout << "Unsigned Long value: " << allSet.to_ulong();
return 0;
}
Output
any() value: true all() value: true none() value: false test() at index 0: 1 size() value: 4 Value of allUnset on before using set(): 0000 Value of allUnset on after using set(): 0001 Value of allSet on before using reset(): 1111 Value of allSet on after using reset(): 1110 bitString: 1110 Unsigned Long value: 14
Note: boolalpha is used to print “true” and “false” instead of 1 or 0 for boolean values and noboolalpha for opposite.
C++ bitset and its application
A bitset is an array of bools but each boolean value is not stored in a separate byte instead, bitset optimizes the space such that each boolean value takes 1-bit space only, so space taken by bitset is less than that of an array of bool or vector of bool.
A limitation of the bitset is that size must be known at compile time i.e. size of the bitset is fixed.
std::bitset is the class template for bitset that is defined inside <bitset> header file so we need to include the header file before using bitset in our program.
Syntax:
bitset<size> variable_name(initialization);
We can initialize bitset in three ways :
1. Uninitialized: All the bits will be set to zero.
bitset<size> variable_name;
2. Initialization with decimal integer: Bitset will represent the given decimal number in binary form.
bitset<size> variable_name(DECIMAL_NUMBER);
3. Initialization with binary string: Bitset will represent the given binary string.
bitset<size> variable_name(string("BINARY_STRING"));
bitset<size> variable_name("BINARY_STRING");
Example:
// C++ program to demonstrate the bitset
#include <bitset>
#include <iostream>
using namespace std;
int main()
{
// declaring an uninitialized bitset object
bitset<8> uninitializedBitset;
// initialization with decimal number
bitset<8> decimalBitset(15);
// initialization with binary string
bitset<8> stringBitset(string("1111"));
cout << "Uninitialized bitset: " << uninitializedBitset
<< endl;
cout << "Initialized with decimal: " << decimalBitset
<< endl;
cout << "Initialized with string: " << stringBitset
<< endl;
return 0;
}
Output
Uninitialized bitset: 00000000 Initialized with decimal: 00001111 Initialized with string: 00001111
Contact Us