<xstd/ints/bit/countl_zero.hpp>¶
This page also specifies <xstd/ints/bit/countl_one.hpp>, <xstd/ints/bit/countr_one.hpp>, <xstd/ints/bit/countr_zero.hpp> and <xstd/ints/bit/popcount.hpp>, whose functions share this text.
xstd::countl_zero, xstd::countl_one, xstd::countr_zero, xstd::countr_one, xstd::popcount¶
// (1)
template<class T>
constexpr auto countl_zero(T x) noexcept(see below) -> int;
// (2)
template<class T>
constexpr auto countl_one(T x) noexcept(see below) -> int;
// (3)
template<class T>
constexpr auto countr_zero(T x) noexcept(see below) -> int;
// (4)
template<class T>
constexpr auto countr_one(T x) noexcept(see below) -> int;
// (5)
template<class T>
constexpr auto popcount(T x) noexcept(see below) -> int;
Count the bits of an unsigned integer's value, for every type the standard functions take and every
unsigned_integer.
countl_zerocounts the leading0bits.countl_onecounts the leading1bits.countr_zerocounts the trailing0bits.countr_onecounts the trailing1bits.popcountcounts the1bits.
Parameters¶
| Parameter | Meaning |
|---|---|
x |
the value whose bits are counted |
Constraints: For each function f, std::f(x) is well-formed, or T models unsigned_integer.
Return value¶
Where std::f(x) is well-formed, each returns std::f(x).
- Returns: The number of consecutive
0bits in the value ofx, starting from the most significant bit, soNfor0. - Returns: The number of consecutive
1bits in the value ofx, starting from the most significant bit. - Returns: The number of consecutive
0bits in the value ofx, starting from the least significant bit, soNfor0. - Returns: The number of consecutive
1bits in the value ofx, starting from the least significant bit. - Returns: The number of
1bits in the value ofx.
Exceptions¶
- The exception specification is
truefor the forwarding overload and the builtin one, and otherwisenothrow_const_operators<T>. - The exception specification is
truefor the forwarding overload, and otherwisenothrow_const_operators<T>. - As (1).
- As (2).
- As (1).
Remarks¶
Where XSTD_HAS_BIT_INT is defined, std::f refuses bit_uint<N>, and __has_builtin reports __builtin_clzg,
__builtin_ctzg or __builtin_popcountg respectively, countl_zero, countr_zero and popcount have a third
overload taking bit_uint<N> and deducing N, which returns the builtin's answer, N for zero where it counts zeros,
and is noexcept. It is more specialized than the block template, so for each width exactly one of the three
overloads is viable.
Notes¶
- A signed 128-bit class has no bits to count, as a signed built-in has none for
std::popcount. - A call is a constant expression.
See also¶
| Entity | Relation |
|---|---|
bit_width |
the position past the highest 1 bit |
bit_select |
the position of the 1 bit of a given rank |
unsigned_integer |
what the block template takes |
std::countl_zero |
the standard function (1) forwards to |
std::countl_one |
the standard function (2) forwards to |
std::countr_zero |
the standard function (3) forwards to |
std::countr_one |
the standard function (4) forwards to |
std::popcount |
the standard function (5) forwards to |