Reference
C Library
IOstream Library
Strings library
STL Containers
STL Algorithms
Miscellaneous
STL Containers
bitset
deque
list
map
multimap
multiset
priority_queue
queue
set
stack
vector
bitset
bitset::bitset
bitset operators
member functions:
bitset::any
bitset::count
bitset::flip
bitset::none
bitset::operator[]
bitset::reset
bitset::set
bitset::size
bitset::test
bitset::to_string
bitset::to_ulong


bitset::count

public member function
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