• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <bit>

    std::countl_one

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

    概要

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

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

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

    戻り値

    xの、最上位ビット (MSB, most 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>(0b1110'0000'0000'0000'0000'0000'0000'0000u);
      int n = std::countl_one(i);
      assert(n == 3);
    
      assert(std::countl_one(static_cast<std::uint32_t>(0u)) == 0);
      assert(std::countl_one(static_cast<std::uint32_t>(0b1000'0000'0000'0000'0000'0000'0000'0000u)) == 1);
      assert(std::countl_one(static_cast<std::uint32_t>(0b1111'1111'1111'1111'1111'1111'1111'1111u)) == 32);
    }
    

    出力

    バージョン

    言語

    • C++20

    処理系

    参照