C++ Bitset Library - count() Function


Advertisements

Description

The C++ function std::bitset::count() count number of set bits from bitset.

Declaration

Following is the declaration for std::bitset::count() function form std::bitset header.

C++98

size_t count() const;

C++11

size_t count() const noexcept;

Parameters

None

Return value

Returns number of set bits.

Exceptions

This member function never throws exception.

Example

The following example shows the usage of std::bitset::count() function.

#include <iostream>
#include <bitset>

using namespace std;

int main(void) {

   bitset<4> b("1110");

   cout << "In bitset " << b << ", " << b.count() << " bits are set." << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

In bitset 1110, 3 bits are set.
bitset.htm
Advertisements