std::popcount
提供: cppreference.com
ヘッダ <bit> で定義 | ||
template<class T> constexprint popcount(T x)noexcept; | (C++20以上) | |
x
の値の中の1のビットの数を返します。
このオーバーロードは、T
が符号なし整数型 (すなわち unsignedchar、 unsignedshort、 unsignedint、 unsignedlong、 unsignedlonglong、または拡張符号なし整数型) である場合にのみ、オーバーロード解決に参加します。
目次 |
[編集]引数
x | - | 符号なし整数型の値 |
[編集]戻り値
x の値の中の1のビットの数。
[編集] 例
Run this code
#include <bit>#include <bitset>#include <cstdint>#include <initializer_list>#include <iostream> int main(){for(std::uint8_t i :{0, 0b11111111, 0b00011101}){std::cout<<"popcount(0b"<<std::bitset<8>(i)<<") = "<< std::popcount(i)<<'\n';}}
出力:
popcount(0b00000000) = 0 popcount(0b11111111) = 8 popcount(0b00011101) = 4
[編集] 関連項目
(C++20) | 最上位ビットから連続する0のビットの数を数えます (関数テンプレート) |
(C++20) | 最上位ビットから連続する1のビットの数を数えます (関数テンプレート) |
(C++20) | 最下位ビットから連続する0のビットの数を数えます (関数テンプレート) |
(C++20) | 最下位ビットから連続する1のビットの数を数えます (関数テンプレート) |