GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::bitset::count(3) C++ Standard Libary std::bitset::count(3)

std::bitset::count - std::bitset::count


std::size_t count() const; (until C++11)
std::size_t count() const noexcept; (since C++11)
(until C++23)
constexpr std::size_t count() const noexcept; (since C++23)


Returns the number of bits that are set to true.


(none)


number of bits that are set to true.

// 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;
}


}


initial value: 00010010
setting bit 0: 00010011
setting bit 2: 00010111


size returns the number of bits that the bitset holds
(public member function)
popcount counts the number of 1 bits in an unsigned integer
(C++20) (function template)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.