C++ bitset interesting facts?


C++ programming language defines a container in c++ standard Template Library named as bitset. This bitset container is used in order to work on elements at the bit level i.e. each bit of the variable the bits i.e. binary conversion of the given value.

1. Bitset is like an string − Bitset is a container of bits ( only 0 and 1 are valid ). You can create a bitset with any set of bits specified by start index value of the bitset and the number of elements that are considered i.e. you can create a bitset with 2 elements starting from index 1 of the bitset and append it to the end of the bitset.

Example − we need to feet 4 elements starting from index value 2 of the bit string 01001110. This bitset will take elements 0011 which will be appended to the end of the bitset. So, the value of 8 bit bitset defined by this method is 00000011.

Example

 Live Demo

#include <bitset>
#include <string>
#include <iostream>
int main() {
   std::string bit_string = "10010110";
   std::bitset<8> b1(bit_string, 1 , 4);
   std::cout << b1 << '\n' ;
   return 0;
}

Output

00000010

2. Constructing a Bitset from string − If you have a string that has only two types of values that are used while creation. You can convert this string into a bitset that considers values as there respective 0/1 representation.

Let's take an example to understand this concept better,

We have a string ‘xyxxyyx’, from this we can create a bitset with the same length as the array considering x = 0 and y= 1. The bitset created as 0100110.

There is a constructor defined in the library to perform this task −

bitset(str, offSet, size, zeroVal , oneVal) ;

This is a constructor that is defined to create a bitset. Let’s dig in deep into the constructor and learn what each parameter of the constructor conveys.

str − The string that is to be considered for creating the bitset.

offSet − string index of the string.

size − Size of the bitset that is to be created.

zeroVal − The value of the string that will be considered 0

oneVal − The value of the string that will be considered 1]

Example

 Live Demo

#include <bitset>
#include <string>
#include <iostream>
using namespace std;
int main() {
   string bitstr = "xyxxyyyx";
   bitset<8> bits(bitstr, 0, bitstr.size(), 'x', 'y');
   cout <<"The bitset is : "<< bits << '\n';
}

Output

The bitset is: 01001110

3. Convert bitset to string − In bitset, there is a function to convert a bitset to a string. The function to_string() is used to store the values of the bitset into a string. The length of the string is the same as the length of the bitset.

The order storing the elements of the bit set into the string is the same as the bitset order i.e. the first element of the bitset first element of the string.

String conversion of 01010100 is “01010100”.

You can replace 0 and 1 by any letter by specifying in the parameter list of the method. It is just the reverse of what we learned while construction of bitset.

Example

 Live Demo

#include <iostream>
#include <bitset>
using namespace std;
int main() {
   bitset<8> b(19);
   cout<<"The value of the bitset is : "<<b<<endl;
   cout<<"The string conversion of the bitset is : "<<b.to_string()<<endl;
   cout<<"The string conversion by replacing 0 with T and 1 with P is : ";
   cout<< b.to_string('T', 'P')<<endl;
}

Output

The value of bitset is : 00010011
The string conversion of bitset is : 00010011
string conversion by replacing 0 with T and 1 with P is : TTTPTTPP

There are many other operations that are done on bitset. also some bit operators are also there that can be used. these are basic functions and properties of bitset that are discussed here.

Updated on: 04-Oct-2019

195 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements