Std::bitset
lastmod 2018-08-17created 2015-04-083 edits
Syntax
Pass the number of bits as the template argument. The size must be fixed at Compile time. If you want to decide it at Run time, use Dynamic bitset.
Dynamic bitset
Included in Boost.
Methods
-
bool bitset<N>::any() -
trueif any value istrue, otherwisefalse.
-
bool bitset<N>::test(i) - Returns the i-th value.
-
bitset<N>&bitset<N>::set(i,b) - Sets the i-th value to b. If b is omitted, the default is 1.
-
bitset<N>&bitset<N>::reset(i) - Sets the i-th value to 0.
-
size_t bitset<N>::count() - Returns the number of 1s.
Example
#include <iostream>
#include <bitset>
using namespace std;
int main() {
unsigned short short1 = 4;
bitset<16> bitset1{short1}; // the bitset representation of 4
cout << bitset1 << endl; // 0000000000000100
unsigned short short2 = short1 << 1; // 4 left-shifted by 1 = 8
bitset<16> bitset2{short2};
cout << bitset2 << endl; // 0000000000001000
unsigned short short3 = short1 << 2; // 4 left-shifted by 2 = 16
bitset<16> bitset3{short3};
cout << bitset3 << endl; // 0000000000010000
}