名前空間
変種
操作

std::bit_floor

提供: cppreference.com
< cpp‎ | numeric
ヘッダ <bit> で定義
template< class T >
constexpr T bit_floor(T x) noexcept;
(C++20以上)

x がゼロでなければ、 x より大きくない最も大きな2の整数乗を計算します。 x がゼロの場合はゼロを返します。

このオーバーロードは、T が符号なし整数型 (つまり unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long または拡張符号なし整数型) である場合にのみ、オーバーロード解決に参加します。

[編集] 戻り値

x がゼロであればゼロ、そうでなければ x より大きくない最も大きな2の整数乗。

[編集]

#include <bit>
#include <bitset>
#include <iostream>
 
auto main() -> int
{
    using bin = std::bitset<8>;
 
    for (unsigned x = 0; x != 10; ++x)
    {
        auto const z = std::bit_floor(x); // `floor2` before P1956R1
 
        std::cout << "bit_floor(" << bin(x) << ") = " << bin(z) << '\n';
    }
}

出力:

bit_floor(00000000) = 00000000
bit_floor(00000001) = 00000001
bit_floor(00000010) = 00000010
bit_floor(00000011) = 00000010
bit_floor(00000100) = 00000100
bit_floor(00000101) = 00000100
bit_floor(00000110) = 00000100
bit_floor(00000111) = 00000100
bit_floor(00001000) = 00001000
bit_floor(00001001) = 00001000