std::countl_zero(3)

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

πŸ’… std::countl_zero πŸ’…

okay bestie, this one is from the C++ Standard Libary manual β€” yes, that spelling is literally on the page, don't come for me 😭. It's giving bit-counting main character energy, and it's here to count leading zeros for you, no cap πŸ’–.

πŸŽ€ NAME πŸŽ€

std::countl_zero - std::countl_zero

✨ Synopsis ✨

First up, the official setup:

Defined in header <bit>
template< class T >                         (since C++20)
constexpr int countl_zero( T x ) noexcept;

It returns the number of consecutive 0 bits in the value of x, starting from the most significant bit β€” that's the "left" side, iykyk πŸ‘‘.

This overload participates in overload resolution only if T is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type). Periodt.

🌸 Parameters 🌸

  • x β€” value of unsigned integer type 🫢

πŸ¦‹ Return value πŸ¦‹

The number of consecutive 0 bits in the value of x, starting from the most significant bit. Literally the whole vibe. ✨

⭐ Notes ⭐

Feature-test macro check, bestie:

  • __cpp_lib_bitops β€” value 201907L, std (C++20), feature Bit operations 🌈

πŸ”₯ Example πŸ”₯

This example is serving 8-bit realness β€” run it and see:

       // Run this code

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

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

πŸ“ Output πŸ“

countl_zero( 00000000 ) = 8
countl_zero( 11111111 ) = 0
countl_zero( 11110000 ) = 0
countl_zero( 00011110 ) = 3

πŸ‘‘ See also πŸ‘‘

  • 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) 🌸
  • popcount (C++20) β€” counts the number of 1 bits in an unsigned integer (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>) πŸ¦‹

That's the tea from http://cppreference.com, version date 2024.06.10, page std::countl_zero(3) β€” bye bestie, slay your bit ops πŸ’…βœ¨

πŸ“¦ where this page comes from