std::countl_one(3)

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

πŸ‘‘ std::countl_one (3) πŸ‘‘

manual: C++ Standard Libary πŸ’…

okay bestie, welcome to the C++ Standard Libary page for std::countl_one β€” yes, the manual name is literally β€œC++ Standard Libary,” and we are keeping it exactly as-is. This one is all about counting consecutive 1 bits from the left side, and it’s lowkey iconic. πŸ¦‹


✨ NAME ✨

std::countl_one - std::countl_one

✨ Synopsis ✨

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

Returns the number of consecutive 1 ("one") bits in the value of x, starting from the most significant bit ("left"). 🌸

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).

🫢 Parameters 🫢

  • x β€” value of unsigned integer type

πŸŽ€ Return value πŸŽ€

The number of consecutive 1 bits in the value of x, starting from the most significant bit. No cap, that’s just what it returns. πŸ’–

🧚 Notes 🧚

Feature-test macro vibe check:

  • 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>

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

🌈 Output 🌈

countl_one( 00000000 ) = 0
countl_one( 11111111 ) = 8
countl_one( 01111111 ) = 0
countl_one( 11100011 ) = 3

πŸ‘‘ See also πŸ‘‘

  • countl_zero β€” counts the number of consecutive 0 bits, starting from the most significant bit (C++20) (function template)
  • countr_zero β€” counts the number of consecutive 0 bits, starting from the least 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)
  • has_single_bit β€” checks if a number is an integral power of 2 (C++20) (function template)
  • count β€” returns the number of bits set to true (public member function of std::bitset<N>)
  • all β€” checks if all, any or none of the bits are set to true (public member function of std::bitset<N>)
  • any β€” checks if all, any or none of the bits are set to true (public member function of std::bitset<N>)
  • none β€” checks if all, any or none of the bits are set to true (public member function of std::bitset<N>)

okay that’s the whole page bestie! source/version: http://cppreference.com; date: 2024.06.10; page: std::countl_one(3) β€” mwah πŸ’‹βœ¨

πŸ“¦ where this page comes from