std::bitset<N>::count (3) - Linux Manuals
std::bitset<N>::count: std::bitset<N>::count
Command to display std::bitset<N>::count
manual in Linux: $ man 3 std::bitset<N>::count
NAME
std::bitset<N>::count - std::bitset<N>::count
Synopsis
std::size_t count() const; (until C++11)
std::size_t count() const noexcept; (since C++11)
Returns the number of bits that are set to true.
Parameters
(none)
Return value
number of bits that are set to true.
Example
// Run this code
#include <iostream>
#include <bitset>
int main()
{
std::bitset<8> b("00010010");
std::cout << "initial value: " << b << '\n';
// find the first unset bit
std::size_t idx = 0;
while (idx < b.size() && b.test(idx)) ++idx;
// continue setting bits until half the bitset is filled
while (idx < b.size() && b.count() < b.size()/2) {
b.set(idx);
std::cout << "setting bit " << idx << ": " << b << '\n';
while (idx < b.size() && b.test(idx)) ++idx;
}
}
Output:
initial value: 00010010
setting bit 0: 00010011
setting bit 2: 00010111
See also
returns the size number of bits that the bitset can hold
size (public member function)
Pages related to std::bitset<N>::count
- std::bitset<N>::all,std::bitset<N>::any,std::bitset<N>::none (3) - std::bitset<N>::all,std::bitset<N>::any,std::bitset<N>::none
- std::bitset<N>::bitset (3) - std::bitset<N>::bitset
- std::bitset<N>::flip (3) - std::bitset<N>::flip
- std::bitset<N>::operator&=,|=,^=,~ (3) - std::bitset<N>::operator&=,|=,^=,~
- std::bitset<N>::operator<<,<<=,>>,>>= (3) - std::bitset<N>::operator<<,<<=,>>,>>=
- std::bitset<N>::operator[] (3) - std::bitset<N>::operator[]
- std::bitset<N>::reference (3) - std::bitset<N>::reference
- std::bitset<N>::reset (3) - std::bitset<N>::reset
- std::bitset<N>::set (3) - std::bitset<N>::set
- std::bitset<N>::size (3) - std::bitset<N>::size
- std::bitset<N>::test (3) - std::bitset<N>::test
- std::bitset<N>::to_string (3) - std::bitset<N>::to_string
- std::bitset<N>::to_ullong (3) - std::bitset<N>::to_ullong
- std::bitset<N>::to_ulong (3) - std::bitset<N>::to_ulong
- std::bitset (3) - std::bitset