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 (「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> の公開メンバ関数)