std::popcount
ja.cppreference.net から
| ヘッダ <bit>
|
||
template< class T >
constexpr int popcount( T x ) noexcept;
|
(C++20 以上) で定義 | |
x の値の 1 ビットの数を返す。x
このオーバーロードは、T が符号なし整数型(つまり、unsigned char、unsigned short、unsigned int、unsigned long、unsigned long long、または拡張符号なし整数型)である場合にのみオーバーロード解決に参加する。
パラメータ
| x | - | 符号なし整数型の値 |
戻り値
1 ビットの数(値 x 内)。
注記
名前
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>の公開メンバ関数)
|