Usage of Bitset STL Container

Learn about the usage of bitset STL containers that store boolean values.

We'll cover the following


Write a program that uses a bitset STL container to carry out the following operations:

  • Set up a bit-pattern of desired size and value
  • Count the number of 0s and 1s in a bit pattern
  • Check whether any bit in a bit-pattern is set or not
  • Set a specific value at the desired position in a bit-pattern
  • Flip individual or all the bits in a bit-pattern
  • Reset individual or all the bits in a bit-pattern
  • Obtain binary equivalent of a decimal number

Sample run

Here’s what you should see when you run the program.

bs1 = 00000000
bs2 = 00101101
bs3 = 00101100
bs4 = 00000000
bs5 = 11111111
Number of 1s in bs3 = 3
Number of 0s in bs3 = 5
bs1 has no bit set
bs5 has all bit set
bs1 after setting all bits = 11111111
bs4 after setting 00001111 = 00001111
bs4 after resetting bit 1 = 00001101
bs4 after resetting all bits = 00000000
2nd bit is bs4 is off
Current bs4 value = 00000000
bs4 after flipping bit 3 = 00001000
bs4 after flipping all bits = 11110111
Decimal number = 65
Binary equivalent = 01000001

Coding solution

Here is a solution to the problem above.

Get hands-on with 1200+ tech skills courses.