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

履歴 編集

function template
<bit>

std::bit_floor(C++20)

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

概要

整数値を2の累乗値に切り下げる。

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

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

戻り値

x == 0である場合、0を返す。そうでない場合、has_single_bit(y) == trueおよびy <= xとなるような最大のyを求めて返す。

例外

投げない

#include <iostream>
#include <bit>

void convert_to_pow2(unsigned int x)
{
  std::cout << x << "\t : " << std::bit_floor(x) << std::endl;
}

int main()
{
  std::cout << "129\t : " << std::bit_floor(129u) << std::endl;

  for (unsigned int i = 0u; i <= 34u; ++i) {
    convert_to_pow2(i);
  }
}

出力

129  : 128
0    : 0
1    : 1
2    : 2
3    : 2
4    : 4
5    : 4
6    : 4
7    : 4
8    : 8
9    : 8
10   : 8
11   : 8
12   : 8
13   : 8
14   : 8
15   : 8
16   : 16
17   : 16
18   : 16
19   : 16
20   : 16
21   : 16
22   : 16
23   : 16
24   : 16
25   : 16
26   : 16
27   : 16
28   : 16
29   : 16
30   : 16
31   : 16
32   : 32
33   : 32
34   : 32

バージョン

言語

  • C++20

処理系

参照