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
.
(none).
number of bits that are set to true
.
#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
returns the size number of bits that the bitset can hold (public member function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/cpp/utility/bitset/count