std:: bit_floor
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++20)
|
||||
|
(C++23)
|
||||
| Integral powers of 2 | ||||
|
(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)
|
|
定义于头文件
<bit>
|
||
|
template
<
class
T
>
constexpr T bit_floor ( T x ) noexcept ; |
(C++20 起) | |
如果 x 不为零,则计算不大于 x 的最大二次幂整数。如果 x 为零,则返回零。
此重载仅当
T
为无符号整数类型(即
unsigned
char
、
unsigned
short
、
unsigned
int
、
unsigned
long
、
unsigned
long
long
或扩展无符号整数类型)时参与重载决议。
目录 |
参数
| x | - | 无符号整数值 |
返回值
若 x 为零则返回零;否则返回不大于 x 的最大二次幂整数。
注释
在
P1956R1
之前,此函数模板的建议名称为
floor2
。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_int_pow2
|
202002L
|
(C++20) | 整型二次幂运算 |
可能的实现
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; } |
示例
#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'; }
输出:
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
另请参阅
|
(C++20)
|
寻找不小于给定值的最小2的整数次幂
(函数模板) |
|
(C++20)
|
计算按位右旋转的结果
(函数模板) |
|
(C++20)
|
寻找表示给定值所需的最小位数
(函数模板) |
|
(C++20)
|
检查一个数是否为2的整数次幂
(函数模板) |