|
|
(C++20) | ||||
(C++23) | ||||
Integral powers of2 | ||||
(C++20) | ||||
(C++20) | ||||
bit_floor (C++20) | ||||
(C++20) | ||||
Rotating | ||||
(C++20) | ||||
(C++20) | ||||
Counting | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
(C++20) | ||||
Endian | ||||
(C++20) |
Defined in header <bit> | ||
template<class T> constexpr T bit_floor( T x)noexcept; | (since C++20) | |
Ifx is not zero, calculates the largest integral power of two that is not greater thanx. Ifx is zero, returns zero.
This overload participates in overload resolution only ifT
is an unsigned integer type (that is,unsignedchar,unsignedshort,unsignedint,unsignedlong,unsignedlonglong, or an extended unsigned integer type).
Contents |
x | - | unsigned integer value |
Zero ifx is zero; otherwise, the largest integral power of two that is not greater thanx.
Prior toP1956R1, the proposed name for this function template wasfloor2
.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_int_pow2 | 202002L | (C++20) | Integral power-of-2 operations |
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);return0;} |
#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 ) = 00000000bit_floor( 00000001 ) = 00000001bit_floor( 00000010 ) = 00000010bit_floor( 00000011 ) = 00000010bit_floor( 00000100 ) = 00000100bit_floor( 00000101 ) = 00000100bit_floor( 00000110 ) = 00000100bit_floor( 00000111 ) = 00000100bit_floor( 00001000 ) = 00001000bit_floor( 00001001 ) = 00001000
(C++20) | finds the smallest integral power of2 not less than the given value (function template)[edit] |
(C++20) | computes the result of bitwise right-rotation (function template)[edit] |
(C++20) | finds the smallest number of bits needed to represent the given value (function template)[edit] |
(C++20) | checks if a number is an integral power of2 (function template)[edit] |