std:: popcount
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
(C++20)
|
||||
|
(C++23)
|
||||
| Integral powers of 2 | ||||
|
(C++20)
|
||||
|
(C++20)
|
||||
|
(C++20)
|
||||
|
(C++20)
|
||||
| Rotating | ||||
|
(C++20)
|
||||
|
(C++20)
|
||||
| Counting | ||||
|
(C++20)
|
||||
|
(C++20)
|
||||
|
(C++20)
|
||||
|
(C++20)
|
||||
|
popcount
(C++20)
|
||||
| Endian | ||||
|
(C++20)
|
|
定义于头文件
<bit>
|
||
|
template
<
class
T
>
constexpr int popcount ( T x ) noexcept ; |
(C++20 起) | |
返回 x 值中 1 位的数量。
此重载仅当
T
为无符号整数类型(即
unsigned
char
、
unsigned
short
、
unsigned
int
、
unsigned
long
、
unsigned
long
long
或扩展无符号整数类型)时参与重载决议。
目录 |
参数
| x | - | 无符号整数类型的值 |
返回值
值 x 中 1 比特的数量。
注释
名称
popcount
是“population count”的缩写。
| 功能测试 宏 | 值 | 标准 | 功能 |
|---|---|---|---|
__cpp_lib_bitops
|
201907L
|
(C++20) | 位操作 |
示例
#include <bit> #include <bitset> #include <cstdint> #include <iostream> static_assert(std::popcount(0xFULL) == 4); int main() { for (const std::uint8_t x : {0, 0b00011101, 0b11111111}) std::cout << "popcount( " << std::bitset<8>(x) << " ) = " << std::popcount(x) << '\n'; }
输出:
popcount( 00000000 ) = 0 popcount( 00011101 ) = 4 popcount( 11111111 ) = 8
参见
|
(C++20)
|
从最高有效位开始统计连续
0
位的数量
(函数模板) |
|
(C++20)
|
从最高有效位开始统计连续
1
位的数量
(函数模板) |
|
(C++20)
|
从最低有效位开始统计连续
0
位的数量
(函数模板) |
|
(C++20)
|
从最低有效位开始统计连续
1
位的数量
(函数模板) |
|
(C++20)
|
检查一个数是否为
2
的整数次幂
(函数模板) |
|
返回被设置为
true
的位的数量
(
std::bitset<N>
的公开成员函数)
|
|
|
检查是否所有、任一或没有位被设置为
true
(
std::bitset<N>
的公开成员函数)
|