std::popcount(3)
β¨ std::popcount β¨
okay bestie, so this little icon lives in the C++ Standard Library (yes, that's the manual name, we're fancy here π ) and honestly? it's giving "tiny function, huge brain." Let's get into it π«Ά
πΈ NAME πΈ
std::popcount β literally just std::popcount. That's the name. That's the whole name section. iconic of it, honestly π
π¦ Synopsis π¦
Defined in header <bit>
template< class T > (since C++20)
constexpr int popcount( T x ) noexcept;
Returns the number of 1 bits in the value of x. that's it. that's the function. no cap π
β οΈ okay this part matters so listen up: this overload only participates in overload resolution ifTis an unsigned integer type β and "unsigned integer type" here meansunsigned char,unsigned short,unsigned int,unsigned long,unsigned long long, or an extended unsigned integer type. signed types? not invited to the party π
π Parameters π
xβ value of unsigned integer type
π Return value π
The number of 1 bits in the value of x. so like, you hand it a number, it counts the 1s. very demure, very mindful π§
π Notes π
The name popcount is a contraction for "population count" β and now you know, so iykyk β¨
Feature-test macro vibes, keep these exactly as they are:
- Feature-test macro:
__cpp_lib_bitops - Value:
201907L - Std:
(C++20) - Feature: Bit operations
π₯ Example π₯
// Run this code
#include <bit>
#include <bitset>
#include <cstdint>
#include <iostream>
static_assert(std::popcount(0xFULL) == 4);
int main()
{
for (const std::uint8_t x : {0, 0b00011101, 0b11111111})
std::cout << "popcount( " << std::bitset<8>(x) << " ) = "
<< std::popcount(x) << '\n';
}
lowkey thatstatic_assertis a whole mood β0xFULLhas 4 set bits and the compiler checks it before the program even runs. obsessed π
β Output β
popcount( 00000000 ) = 0
popcount( 00011101 ) = 4
popcount( 11111111 ) = 8
look at that. zero 1s β 0, four 1s β 4, all eight β 8. the math is mathing, bestie π
π¦ See also π¦
countl_zero(C++20)β counts the number of consecutive0bits, starting from the most significant bit (function template)countl_one(C++20)β counts the number of consecutive1bits, starting from the most significant bit (function template)countr_zero(C++20)β counts the number of consecutive0bits, starting from the least significant bit (function template)countr_one(C++20)β counts the number of consecutive1bits, starting from the least significant bit (function template)has_single_bit(C++20)β checks if a number is an integral power of 2 (function template)countβ returns the number of bits set totrue(public member function ofstd::bitset<N>)all,any,noneβ checks if all, any or none of the bits are set totrue(public member function ofstd::bitset<N>)
π« that's the whole page, bestie β go count some bits and slay π«
source: http://cppreference.com Β· version date: 2024.06.10 Β· page: std::popcount(3) β mwah, see you in the next manpage ππ
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms