CPP Tutorial

CPP examples programs

CPP Bitset

CPP provides various bitset functions. Some of these are:

FUNCTION USES
all() To test whether all bits from bitset are set or not.
any() To test whether at least one bit from bitset is set or not.
count() To count the number of set bits.
flip() To flip all the bit values converting zeros to ones and ones to zeros.
none() To check whether no bit is set.
operator[] To access value to any index of a bitset.
reset() To reset all the bits of bitset.
size() To check the size of the bitset.
set() To set all the bits to 1.
test() To test whether the bit at position p is set or not.
to_string() To construct a basic string object that represents the bits in the bitset.
to_ulong() To convert the contents of the bitset to an unsigned long long integer.

Example:

#include <iostream>  
#include <bitset>  
using namespace std;  
int main()  
{  
bitset<8> num(string("11011011"));  
cout<< "Actual bits : " << num <<'\n';  
cout<< "Set bits : " <<num.set() <<'\n';
cout<< "Reset bits : " <<num.reset();
return 0;  
}

Output

Actual bits : 11011011             
Set bits : 11111111       
Reset bits : 00000000