bitset - STL C++

bitset<N>::count()

Declaration

size_t bitset<N>::count() const;

Description

This is the member function count() for the bitset class template. This function returns the number of bits in the bitset that are equal to 1.

Header Include

#include <bitset>

Example

#include <bitset>
#include <iostream>

int main()
{
	using namespace std;

	// Create two bitsets
	bitset<4> qBits1(3);
	bitset<4> qBits2(11);
	cout << "bitset<4> #1 : "<< qBits1 << endl;
	cout << "bitset<4> #2 : "<< qBits2 << endl;

	// Call the count() function on the bitsets.
	cout << qBits1.count()
		<< " of the bits of bitset<4> #1 are 1." << endl;
	cout << qBits2.count()
		<< " of the bits of bitset<4> #2 are 1." << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

bitset<N>::count() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.