Namespaces
Variants

std:: countr_one

From cppreference.net
Utilities library
定義ヘッダー <bit>
template < class T >
constexpr int countr_one ( T x ) noexcept ;
(C++20以降)

1 ビットが最下位ビット(「右」)から連続して設定されている数を返します。

このオーバーロードは、 T が符号なし整数型(つまり、 unsigned char unsigned short unsigned int unsigned long unsigned long long 、または拡張符号なし整数型)である場合にのみ、オーバーロード解決に参加します。

目次

パラメータ

x - 符号なし整数型の値

戻り値

値の最下位ビットから始まる連続した 1 ビットの数( x の値において)。

注記

機能テスト マクロ 標準 機能
__cpp_lib_bitops 201907L (C++20) ビット操作

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

出力:

countr_one( 00000000 ) = 0
countr_one( 11111111 ) = 8
countr_one( 11111110 ) = 0
countr_one( 11100011 ) = 2

関連項目

最上位ビットから開始して、連続する 0 ビットの数を数える
(関数テンプレート)
(C++20)
最上位ビットから開始して、連続する 1 ビットの数を数える
(関数テンプレート)
最下位ビットから開始して、連続する 0 ビットの数を数える
(関数テンプレート)
(C++20)
符号なし整数における 1 ビットの数を数える
(関数テンプレート)
数値が 2 の整数乗であるかどうかをチェックする
(関数テンプレート)
true に設定されているビットの数を返す
( std::bitset<N> の公開メンバ関数)
すべて、いずれか、またはいずれのビットも true に設定されているかどうかをチェックする
( std::bitset<N> の公開メンバ関数)