public member function
<bitset>

std::bitset::count

size_t count ( );
Count bits set
Returns the amount of bits in the bitset that are set (i.e., have a value of 1).

Parameters

none

Return value

The number of bits set.

size_t is an unsigned integral type.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// bitset::count
#include <iostream>
#include <string>
#include <bitset>
using namespace std;

int main ()
{
  bitset<8> myset (string("10110011"));

  cout << "myset has " << int(myset.count()) << " ones ";
  cout << "and " << int(myset.size()-myset.count()) << " zeros.\n";

  return 0;
}


Output:
myset has 5 ones and 3 zeros.

See also