C++ Standard Library C++ STL Library

C++ <bitset> - size() Function

The C++ bitset::size function returns the number of bits in the bitset.


size_t size() const;
constexpr size_t size() noexcept;


No parameter is required.

Return Value

Number of bits present in the bitset.


Never throws exceptions.


In the example below, the bitset::size function is used to find out the number of bits in the given bitset.

#include <iostream>
#include <bitset>
using namespace std;
int main (){
  bitset<5> bset1;
  bitset<10> bset2;

  cout<<"bset1 contains "<<bset1.size()<<" bits.\n";
  cout<<"bset2 contains "<<bset2.size()<<" bits.\n";

  return 0;

The output of the above code will be:

bset1 contains 5 bits.
bset2 contains 10 bits.

❮ C++ <bitset> Library