std::bitset::bitset in C++

This tutorial will guide you to learn std::bitset::bitset in C++ with examples.

A bitset is an array of boolean values where each boolean value takes only 1-bit space. Bitset stores values in the form of 0 and 1. 0 represents false value or unset value. Whereas 1 represents true value or set value. Space occupied by bitset is less than that of boolean values b[n] and vectors v[n]. However, there is a limitation with bitset unlike dynamic arrays and vectors is that its size is fixed at compile time.

The same information is stored by bitset in a compressed way which makes it faster than vectors and arrays. Since bitset is treated as an array we can access each of its bits using indexing operator the same as an array.

There are many functions available in <bitset> header, which is explained in the below code.

#include <bitset>
#include <iostream> 
using namespace std; 

#define N 32

int main() 
{ 
  // default constructor initializes with all bits 0 
  bitset<N> b1; 

  // b2 is initialized with bits of 20 
  bitset<N> b2(20); 

  // bset3 is initialized with bits of specified binary string 
  bitset<N> b3(string("1100")); 

  // prints exact bits representation of bitset 
  cout << b1 << endl; // 00000000000000000000000000000000 
  cout << b2 << endl; // 00000000000000000000000000010100 
  cout << b3 << endl; // 00000000000000000000000000001100 
  cout << endl; 

  
    // declaring b4 with capacity of 8 bits 
    bitset<8> b4; // 00000000 

  // setting first bit (or 6th index) 
  b4[1] = 1; // 00000010 
  b4[4] = b4[1]; 
  cout << b4 << endl; // 00010010 

  // count function returns number of set bits in bitset 
    // i.e. number of bits in bitset which are one
  int numberof1 = b4.count(); 

  // size function returns total number of bits in bitset 
  // so there difference will give us number of unset(0) 
  // bits in bitset 
  int numberof0 = b4.size() - numberof1; 

  cout << b4 << " has " << numberof1 << " ones and "
    << numberof0 << " zeros\n"; 

  // test function return 1 if bit is set else returns 0 
  cout << "bool representation of " << b4 << " : "; 
  for (int i = 0; i < b4.size(); i++) 
    cout << b4.test(i) << " "; 

  cout << endl; 

  // sets all bits 
  cout << b4.set() << endl; 

  // bitset.set(pos, b) makes bset[pos] = b 
  cout << b4.set(4, 0) << endl; 

  // bitset.set(pos) makes bset[pos] = 1 i.e. default 
  // is 1 
  cout << b4.set(4) << endl; 

  // reset function makes all bits 0 
  cout << b4.reset(2) << endl; 
  cout << b4.reset() << endl; 

  // flip function flips all bits i.e. 1 <-> 0 
  // and 0 <-> 1 
  cout << b4.flip(2) << endl; 
  cout << b4.flip() << endl; 

  // Converting decimal number to binary by using bitset 
  int num = 100; 
  cout << "\nDecimal number: " << num 
    << " Binary equivalent: " << bitset<8>(num); 

  return 0; 
}
Output :
00000000000000000000000000000000
00000000000000000000000000010100
00000000000000000000000000001100

00010010
00010010 has 2 ones and 6 zeros
bool representation of 00010010 : 0 1 0 0 1 0 0 0 
b1 has no bit set.
11111111
11101111
11111111
11111011
00000000
00000100
11111011

Decimal number: 100 Binary equivalent: 01100100

There are many operators available in <bitset> header, which is explained in the below code.

#include <bitset> 
#include <iostream> 
using namespace std; 
// Driver function
int main() 
{ 
  // 1001 is stored in b1
  bitset<4> b1(9);  
  // 0011 is stored in b2
  bitset<4> b2(3); 
  // comparison operator 
  cout << (b1 == b2) << endl; // false 0 
  cout << (b1 != b2) << endl; // true 1 

  // bitwise operation and assignment 
  // Xor operation
  cout << (b1 ^= b2) << endl; // 1010 
  // AND operation
  cout << (b1 &= b2) << endl; // 0010
  // OR operation 
  cout << (b1 |= b2) << endl; // 0011 

  // left and right shifting 
  cout << (b1 <<= 2) << endl; // 1100 
  cout << (b1 >>= 1) << endl; // 0110 

  // not operator 
  cout << (~b2) << endl; // 1100 

  // bitwise operator AND, OR and XOR
  cout << (b1 & b2) << endl; // 0010 
  cout << (b1 | b2) << endl; // 0111 
  cout << (b1 ^ b2) << endl; // 0101 
} 
Output :0
1
1010
0010
0011
1100
0110
1100
0010
0111
0101

Also read: bind function and placeholders in C++

Leave a Reply

Your email address will not be published. Required fields are marked *