std::bit_floor

Header: <bit>

If x is not zero, calculates the largest integral power of two that is not greater than x. If x is zero, returns zero.

# Declarations

template< class T >
constexpr T bit_floor( T x ) noexcept;

(since C++20)

# Parameters

# Return value

Zero if x is zero; otherwise, the largest integral power of two that is not greater than x.

# Notes

Prior to P1956R1, the proposed name for this function template was floor2.

# Example

#include <bit>
#include <bitset>
#include <iostream>
 
int main()
{
    using bin = std::bitset<8>;
    for (unsigned x{}; x != 012; ++x)
        std::cout << "bit_floor( " << bin(x) << " ) = "
                  << bin(std::bit_floor(x)) << '\n';
}

# See also