<xstd/ints/bit/has_single_bit.hpp>¶
This page also specifies <xstd/ints/bit/bit_ceil.hpp>, <xstd/ints/bit/bit_floor.hpp> and <xstd/ints/bit/bit_width.hpp>, whose functions share this text.
xstd::has_single_bit, xstd::bit_ceil, xstd::bit_floor, xstd::bit_width¶
// (1)
template<class T>
constexpr auto has_single_bit(T x) noexcept(see below) -> bool;
// (2)
template<class T>
constexpr auto bit_ceil(T x) -> T;
// (3)
template<class T>
constexpr auto bit_floor(T x) noexcept(see below) -> T;
// (4)
template<class T>
constexpr auto bit_width(T x) noexcept(see below) -> int;
The integral powers of two around an unsigned integer, for every type the standard functions take and every
unsigned_integer.
has_single_bitasks whetherxis a power of two.bit_ceilroundsxup to a power of two.bit_floorroundsxdown to a power of two.bit_widthcounts the bitsxneeds.
Parameters¶
| Parameter | Meaning |
|---|---|
x |
an unsigned integer value |
Constraints: For each function f, std::f(x) is well-formed, or T models unsigned_integer. bool, the
character types and every signed type are refused, as <bit> refuses them; a 128-bit integer class and an
unsigned _BitInt(N) are taken.
Preconditions: (2) The smallest integral power of two not less than x is representable as a value of type T:
x is at most the highest power of two of its type. The block template diagnoses a violation by assert, and the
forwarding overload as std::bit_ceil does.
Return value¶
Where std::f(x) is well-formed, each returns std::f(x).
- Returns:
trueifxis an integral power of two, andfalseotherwise. - Returns: The smallest integral power of two that is not less than
x, so1for both0and1. - Returns:
0ifxis0, and otherwise the largest integral power of two not greater thanx. - Returns:
0ifxis0, and otherwise one plus the base-2 logarithm ofx, with any fractional part discarded, as anint, the type LWG 3656 gavestd::bit_width.
Exceptions¶
- The exception specification is
truefor the forwarding overload, and otherwisenothrow_const_operators<T>. - It is not
noexcept, asstd::bit_ceilis not: its contract is narrow. - The exception specification is
truefor the forwarding overload, and otherwisenothrow_const_operators<T>. - The exception specification is
truefor the forwarding overload, and otherwisenothrow_const_operators<T>.
Remarks¶
(2) A call that violates the precondition is not a core constant expression, whether or not NDEBUG is defined, as
[bit.pow.two] requires of std::bit_ceil.
Notes¶
A call is a constant expression, for bit_ceil one within its precondition.
See also¶
| Entity | Relation |
|---|---|
countl_zero, popcount |
count the bits of the same types |
unsigned_integer |
what the block template takes |
std::has_single_bit |
the standard function (1) forwards to |
std::bit_ceil |
the standard function (2) forwards to |
std::bit_floor |
the standard function (3) forwards to |
std::bit_width |
the standard function (4) forwards to |