std::bitset Operators
Some of the basic operators are overloaded to work with bitset objects. Following is the list of those operators:
Operator | Operation |
---|---|
[] | Access operator |
& | Bitwise AND |
| | Bitwise OR |
! | Bitwise XOR |
>>= | Binary Right shift and assign |
<<= | Binary Left shift and assign |
&= | Assign the value of bitwise AND to the first bitset. |
|= | Assign the value of bitwise OR to the first bitset. |
^= | Assign the value of bitwise XOR to the first bitset. |
~ | Bitwise NOT |
Example:
// C++ program to show the different operator functions on
// bitset
#include <bitset>
#include <iostream>
using namespace std;
int main()
{
bitset<4> bitset1("1001"), bitset2("1010");
bitset<4> result;
cout << "Bitset1: " << bitset1
<< "\nBitset2: " << bitset2 << endl;
cout << "Accessing bit value at index 1 of bitset1: "
<< bitset1[1] << endl;
// bitwise AND
cout << "Bitwise AND using &: "
<< (result = bitset1 & bitset2) << endl;
cout << "Bitwise AND using &=: " << (bitset1 &= bitset2)
<< endl;
// bitwise OR
bitset1 = 9; // 9 = 1001
cout << "Bitwise OR using |: "
<< (result = bitset1 | bitset2) << endl;
cout << "Bitwise OR using |=: " << (bitset1 |= bitset2)
<< endl;
// bitwise NOT
cout << "Bitwise NOT: " << (result = ~bitset1) << endl;
// bitwise XOR
bitset1 = 9;
cout << "Bitwise XOR: " << (bitset1 ^= bitset2) << endl;
bitset1 = 9;
cout << "Binary leftshift on bitwise1: "
<< (bitset1 <<= 1) << endl;
bitset1 = 9;
cout << "Binary rightshift on bitwise1: "
<< (bitset1 >>= 1) << endl;
return 0;
}
Output
Bitset1: 1001
Bitset2: 1010
Accessing bit value at index 1 of bitset1: 0
Bitwise AND using &: 1000
Bitwise AND using &=: 1000
Bitwise OR using |: 1011
Bitwise OR using |=: 1011
Bitwise NOT: 0100
Bitwise XOR: 0011
Binary leftshift on bitwise1: 0010
Binary rightshift on bitwise1: 0100
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