# C++ bitset and its application ?

Server Side ProgrammingProgramming

A bitset is a dataset that stores multiple boolean values but takes lesser memory space as compared to other data sets that can store a sequence of bits like a boolean array or boolean vector.

Bitsets stores the binary bits in a form that takes less memory space, it stores them in compressed from. Accessing any element is same as others i.e. by using its index value i.e. bitset_name[index]. But the indexing of elements in a bitset is reverse. Let’s take an example, for bitset {01101001} the element at 0th index is 1 and so on. So 0’s are at index 1, 2, 4,7. And 1’s are at index 0,3,5,6.

Let’s create a program that will use all functions of the bitset −

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
#define setSize 32
int main() {
bitset<setSize> bset1; // value is 00000000000000000000000000000000
bitset<setSize> bset2(20); //value is 00000000000000000000000000010100
bitset<setSize> bset3(string("1100")); // value is 00000000000000000000000000001100
cout<<"The values of bitsets are :\n" ;
cout<<"bitset 1 : "<<bset1<<endl;
cout<<"bitset 2 : "<<bset2<<endl;
cout<<"bitset 3 : "<<bset3<<endl;
cout << endl;
bitset<8> bset4; // value is 00000000
bset4[1] = 1;
cout<<"value after changing a bit :"<<bset4<<endl;
bset4[4] = bset4[1];
cout <<"changing value using other method :"<<bset4<<endl;
int numberofone = bset4.count();
int numberofzero = bset4.size() - numberofone;
cout<<"The set"<<bset4<<"has"<<numberofone<<"ones and"<<numberofzero<<"zeros\n";
cout << "bool representation of " << bset4 << " : ";
for (int i = 0; i < bset4.size(); i++)
cout << bset4.test(i) << " ";
cout << endl;
if (!bset1.none())
cout << "bset1 has some bit set\n";
cout <<".set() method sets all bits, bset4.set() = "<< bset4.set() << endl;
cout<<"changing a specific bit(4) to 0 "<<bset4.set(4, 0)<<endl;
cout<<"changing a specific bit(4) to 1 "<<bset4.set(4)<<endl;
cout<<"Resetting bit at position 2 :"<<bset4.reset(2)<<endl;
cout<<"Resetting bits of full bitset : "<<bset4.reset()<<endl;
cout<<"Flipping bit at position 2 : "<< bset4.flip(2) << endl;
cout<<"Flipping bit of array : "<<bset4.flip() << endl;
int num = 100;
cout << "\nDecimal number: " << num << " Binary equivalent: " << bitset<8>(num);
return 0;
}

## Output

The values of bitsets are :
bitset 1 : 00000000000000000000000000000000
bitset 2 : 00000000000000000000000000010100
bitset 3 : 00000000000000000000000000001100
value after changing a bit :00000010
changing value using other method :00010010
The set00010010has2ones and6zeros
bool representation of 00010010 : 0 1 0 0 1 0 0 0
.set() method sets all bits, bset4.set() = 11111111
changing a specific bit(4) to 0 11101111
changing a specific bit(4) to 1 11111111
Resetting bit at position 2 :11111011
Resetting bits of full bitset : 00000000
Flipping bit at position 2 : 00000100
Flipping bit of array : 11111011
Decimal number: 100 Binary equivalent: 01100100
Published on 04-Oct-2019 07:04:35