C++ Standard Library C++ STL Library

C++ <bitset> - count() Function

The C++ bitset::count function returns the number of bits in the bitset that are set (i.e., which has a value of 1).


size_t count() const;
size_t count() const noexcept;


No parameter is required.

Return Value

Number of bits in the bitset that are set.


Throws overflow_error, if the bitset size is too big to be represented by the return type.


In the example below, the bitset::count function is used to find out the number of bits in the given bitset which are set.

#include <iostream>
#include <bitset>
using namespace std;
int main (){
  bitset<6> bset("110011");

  cout<<"In bset, "<<bset.count()<<" bits are set.\n";

  return 0;

The output of the above code will be:

In bset, 4 bits are set.

❮ C++ <bitset> Library