std::countr_zero(3)

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

πŸ’… NAME

std::countr_zero β€” bestie's fave little bit-counter, aka std::countr_zero (yes the name is the name, we don't ask questions πŸ’–)


✨ SYNOPSIS

Okay so first things first, this little icon is defined in header <bit> β€” like, that's her home 🏠✨

template< class T >                         (since C++20)
constexpr int countr_zero( T x ) noexcept;

What does she DO though? She returns the number of consecutive 0 bits in the value of x, starting from the least significant bit (the "right" one, iykyk) 🫢

And here's the gatekeeping part β€” this overload only participates in overload resolution if T is an unsigned integer type. And before u ask, "unsigned integer type" means: unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type. That's the whole list, no exceptions, don't @ me πŸ’…


πŸŽ€ PARAMETERS

  • x β€” a value of unsigned integer type. That's literally it, that's the parameter πŸ“

🌸 RETURN VALUE

The number of consecutive 0 bits in the value of x, starting from the least significant bit. Counting from the right side, always, no cap ⭐


πŸ¦‹ NOTES

Feature-test macro vibe check time β€” here's the table bestie:

  • Feature-test macro: __cpp_lib_bitops
  • Value: 201907L
  • Std: (C++20)
  • Feature: Bit operations

So if u ever need to check whether bit ops are chillin in ur standard library, that's ur macro πŸ”₯


🌈 EXAMPLE

Run this code, trust me it's cute:

// Run this code

#include <bit>
#include <bitset>
#include <cstdint>
#include <iostream>

int main()
{
    for (const std::uint8_t i : {0, 0b11111111, 0b00011100,
0b00011101})
        std::cout << "countr_zero( " << std::bitset<8>(i) << " ) = "
                  << std::countr_zero(i) << '\n';
}

⭐ OUTPUT

And she ate, fr fr:

countr_zero( 00000000 ) = 8
countr_zero( 11111111 ) = 0
countr_zero( 00011100 ) = 2
countr_zero( 00011101 ) = 0

Not me looking at 00000000 giving 8 β€” like yeah bestie, all eight of those bits are zero, correct πŸ’€βœ¨


πŸ’œ SEE ALSO

The whole friend group, main character energy each one:

  • countl_zero β€” counts the number of consecutive 0 bits, starting from the most significant bit. (C++20) (function template)
  • countl_one β€” counts the number of consecutive 1 bits, starting from the most significant bit. (C++20) (function template)
  • countr_one β€” counts the number of consecutive 1 bits, starting from the least significant bit. (C++20) (function template)
  • popcount β€” counts the number of 1 bits in an unsigned integer. (C++20) (function template)
  • all, any, none β€” checks if all, any or none of the bits are set to true. (public member function of std::bitset<N>)

Okay so real talk β€” countr_zero is genuinely iconic for bit-twiddling, but remember: she's constexpr and noexcept, so she's safe to use in the chillest of contexts. Slay 🧚

And that's the whole page bestie, no notes πŸ‘‘

Cute little sign-off: this one came from http://cppreference.com, dated 2024.06.10, and it lives at std::countr_zero(3) β€” go drink some water and count some zeros πŸ«ΆπŸ’–πŸŽ€

πŸ“¦ where this page comes from