Namespaces
Variants

std:: countl_one

From cppreference.net
Utilities library
定义于头文件 <bit>
template < class T >
constexpr int countl_one ( T x ) noexcept ;
(C++20 起)

返回从最高有效位(“左侧”)开始,在 x 值中连续 1 (“一”)位的数量。

此重载仅当 T 为无符号整数类型(即 unsigned char unsigned short unsigned int unsigned long unsigned long long 或扩展无符号整数类型)时参与重载决议。

目录

参数

x - 无符号整数类型的值

返回值

x 中从最高有效位开始连续 1 位的数量。

注释

功能测试 标准 功能
__cpp_lib_bitops 201907L (C++20) 位操作

示例

#include <bit>
#include <bitset>
#include <cstdint>
#include <iostream>
int main()
{
    for (const std::uint8_t i : {0, 0b11111111, 0b01111111, 0b11100011})
        std::cout << "countl_one( " << std::bitset<8>(i) << " ) = "
                  << std::countl_one(i) << '\n';
}

输出:

countl_one( 00000000 ) = 0
countl_one( 11111111 ) = 8
countl_one( 01111111 ) = 0
countl_one( 11100011 ) = 3

参阅

从最高有效位开始统计连续 0 位的数量
(函数模板)
从最低有效位开始统计连续 0 位的数量
(函数模板)
(C++20)
从最低有效位开始统计连续 1 位的数量
(函数模板)
(C++20)
统计无符号整数中 1 位的数量
(函数模板)
检查数值是否为 2 的整数次幂
(函数模板)
返回被设置为 true 的位数
( std::bitset<N> 的公开成员函数)
检查是否全部、任一或没有位被设置为 true
( std::bitset<N> 的公开成员函数)