std::popcount(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ 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 if T is an unsigned integer type β€” and "unsigned integer type" here means unsigned 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 that static_assert is a whole mood β€” 0xFULL has 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 consecutive 0 bits, starting from the most significant bit (function template)
  • countl_one (C++20) β€” counts the number of consecutive 1 bits, starting from the most significant bit (function template)
  • countr_zero (C++20) β€” counts the number of consecutive 0 bits, starting from the least significant bit (function template)
  • countr_one (C++20) β€” counts the number of consecutive 1 bits, 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 to true (public member function of std::bitset<N>)
  • all, any, none β€” checks if all, any or none of the bits are set to true (public member function of std::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