std::countr_zero(3)
π 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 consecutive0bits, starting from the most significant bit. (C++20) (function template)countl_oneβ counts the number of consecutive1bits, starting from the most significant bit. (C++20) (function template)countr_oneβ counts the number of consecutive1bits, starting from the least significant bit. (C++20) (function template)popcountβ counts the number of1bits in an unsigned integer. (C++20) (function template)all,any,noneβ checks if all, any or none of the bits are set totrue. (public member function ofstd::bitset<N>)
Okay so real talk βcountr_zerois genuinely iconic for bit-twiddling, but remember: she'sconstexprandnoexcept, 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
- 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