最終更新日時(UTC):
が更新

履歴 編集

function
<bitset>

std::bitset::count

size_t count() const;                    // (1) C++03
size_t count() const noexcept;           // (1) C++11
constexpr size_t count() const noexcept; // (1) C++23

概要

1になっているビットの数を取得する。

戻り値

1になっているビットの数を返す。

基本操作

#include <iostream>
#include <bitset>

int main()
{
  std::bitset<4> bs("1011");

  std::cout << bs.count() << std::endl;
}

出力

3

値が2の乗数かを判定する

#include <iostream>
#include <bitset>
#include <cstdint>

int main()
{
  std::uint32_t ar[] = {4, 8, 16, 32, 24};
  for (std::uint32_t x : ar) {
    // 符号なし整数において、立っているビットがひとつだけなら2の乗数。
    // 立っているビット数を数えることは、popcount (population count) という名前で知られている
    if (std::bitset<32>(x).count() == 1) {
      std::cout << x << " is power of 2" << std::endl;
    }
    else {
      std::cout << x << " is not power of 2" << std::endl;
    }
  }
}

出力

4 is power of 2
8 is power of 2
16 is power of 2
32 is power of 2
24 is not power of 2

関連項目

参照