std::countr_one(3)
π 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β value201907Lβ 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 consecutive0bits, starting from the most significant bit (function template) β since C++20countl_oneβ counts the number of consecutive1bits, starting from the most significant bit (function template) β since C++20countr_zeroβ counts the number of consecutive0bits, starting from the least significant bit (function template) β since C++20popcountβ counts the number of1bits in an unsigned integer (function template) β since C++20has_single_bitβ checks if a number is an integral power of 2 (function template) β since C++20countβ returns the number of bits set to true (public member function ofstd::bitset<N>)allβ checks if all, any or none of the bits are set to true (public member function ofstd::bitset<N>)anyβ checks if all, any or none of the bits are set to true (public member function ofstd::bitset<N>)noneβ checks if all, any or none of the bits are set to true (public member function ofstd::bitset<N>)
lowkey obsessed with howcountl_*looks left andcountr_*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
- 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