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

履歴 編集

function template
<bit>

std::countr_one(C++20)

namespace std {
  template <class T>
  constexpr int countr_one(T x) noexcept;
}

概要

右から連続した1のビットを数える。

テンプレートパラメータ制約

  • Tが符号なし整数型であること

戻り値

xの、最下位ビット (LSB, least significant bit) から開始して連続した1ビットの数を返す。

x == std::numeric_limits<T>::max()の場合、符号なし整数型Tのビット数が返る。

例外

投げない

#include <cassert>
#include <bit>
#include <cstdint>

int main()
{
  auto i = static_cast<std::uint32_t>(0b0000'0000'0000'0000'0000'0000'0000'0111u);
  int n = std::countr_one(i);
  assert(n == 3);

  assert(std::countr_one(static_cast<std::uint32_t>(0u)) == 0);
  assert(std::countr_one(static_cast<std::uint32_t>(0b0000'0000'0000'0000'0000'0000'0000'0001u)) == 1);
  assert(std::countr_one(static_cast<std::uint32_t>(0b1111'1111'1111'1111'1111'1111'1111'1111u)) == 32);
}

出力

バージョン

言語

  • C++20

処理系

参照