template< class T >
constexpr T bit_floor( T x ) noexcept;
If x is not zero, calculates the largest integral power of two that is not greater than x. If x is zero, returns zero.
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).
Zero if x is zero; otherwise, the largest integral power of two that is not greater than x.
[edit] NotesPrior to P1956R1, the proposed name for this function template was floor2
.
template<typename T, typename ... U> concept neither = (!std::same_as<T, U> && ...); template<std::unsigned_integral T> requires neither<T, bool, char, char8_t, char16_t, char32_t, wchar_t> constexpr T bit_floor(T x) noexcept { if (x != 0) return T{1} << (std::bit_width(x) - 1); return 0; }[edit] Example
#include <bit> #include <bitset> #include <iostream> int main() { using bin = std::bitset<8>; for (unsigned x{}; x != 012; ++x) std::cout << "bit_floor( " << bin(x) << " ) = " << bin(std::bit_floor(x)) << '\n'; }
Output:
bit_floor( 00000000 ) = 00000000 bit_floor( 00000001 ) = 00000001 bit_floor( 00000010 ) = 00000010 bit_floor( 00000011 ) = 00000010 bit_floor( 00000100 ) = 00000100 bit_floor( 00000101 ) = 00000100 bit_floor( 00000110 ) = 00000100 bit_floor( 00000111 ) = 00000100 bit_floor( 00001000 ) = 00001000 bit_floor( 00001001 ) = 00001000[edit] See also finds the smallest integral power of 2 not less than the given value
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4