std::countr_one(3)

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

πŸ’… std::countr_one(3)

okay bestie so this page lives in the C++ Standard Libary manual (yes that's how it's spelled in the header, don't come for me), and honestly? It's a little icon. Let's get into it.


✨ NAME ✨

std::countr_one β€” literally std::countr_one, that's the whole name, we're starting strong πŸ’–


🌸 SYNOPSIS 🌸

Defined in header <bit>:

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

So what does it do? It returns the number of consecutive 1 bits in the value of x, starting from the least significant bit (that's the "right" side, iykyk).

Now the important little caveat: this overload only participates in overload resolution if T is an unsigned integer type β€” meaning unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type. No sneaky signed stuff, no cap 🫢


πŸ¦‹ PARAMETERS πŸ¦‹

  • x β€” value of unsigned integer type

That's it. One parameter. Main character energy πŸ‘‘


⭐ RETURN VALUE ⭐

The number of consecutive 1 bits in the value of x, starting from the least significant bit. Same deal as above, just answering the question "okay but what do I get back" πŸ’€


πŸŽ€ NOTES πŸŽ€

Feature-test macro vibes, here's the whole thing:

  • __cpp_lib_bitops β€” value 201907L β€” Std (C++20) β€” Feature: Bit operations

Basically: if you're checking for bitops support, that's your macro πŸ”₯


🌈 EXAMPLE 🌈

Run this code, it's cute:

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

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

πŸ’œ Output

countr_one( 00000000 ) = 0
countr_one( 11111111 ) = 8
countr_one( 11111110 ) = 0
countr_one( 11100011 ) = 2

Look at that. 11111111 giving a full 8, and 11100011 stopping at 2 because it hits a 0 on the third bit from the right. The way the trailing ones just pile up and then get cut off 😭✨


🧚 SEE ALSO 🧚

The whole bit-squad, all (since C++20) unless noted:

  • countl_zero β€” counts the number of consecutive 0 bits, starting from the most significant bit (function template) β€” since C++20
  • countl_one β€” counts the number of consecutive 1 bits, starting from the most significant bit (function template) β€” since C++20
  • countr_zero β€” counts the number of consecutive 0 bits, starting from the least significant bit (function template) β€” since C++20
  • popcount β€” counts the number of 1 bits in an unsigned integer (function template) β€” since C++20
  • has_single_bit β€” checks if a number is an integral power of 2 (function template) β€” since C++20
  • 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>)

lowkey obsessed with how countl_* looks left and countr_* looks right. left-to-right besties fr fr 🫢

okay that's the page!! sourced from http://cppreference.com, dated 2024.06.10, and filed under std::countr_one(3) β€” go count some trailing ones, bestie, you're gonna slay πŸ’…πŸŽ€πŸŒΈ

πŸ“¦ where this page comes from