Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/bit floor"

From cppreference.com
< cpp‎ | numeric
m (Example: C++2a is C++20, use P1956R1 instead)
m (~fmt)
 
(8 intermediate revisions by 3 users not shown)
Line 3: Line 3:
 
{{ddcl|since=c++20|header=bit|
 
{{ddcl|since=c++20|header=bit|
 
template< class T >
 
template< class T >
constexpr T bit_floor(T x) noexcept;
+
constexpr T bit_floor( T x ) noexcept;
 
}}
 
}}
  
If {{tt|x}} is not zero, calculates the largest integral power of two that is not greater than {{tt|x}}. If {{tt|x}} is zero, returns zero.  
+
If {{c|x}} is not zero, calculates the largest integral power of two that is not greater than {{c|x}}. If {{c|x}} is zero, returns zero.
  
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c|unsigned char}}, {{c|unsigned short}}, {{c|unsigned int}}, {{c|unsigned long}}, {{c|unsigned long long}}, or an extended unsigned integer type)}}.
+
{{cpp/enable_if|{{tt|T}} is an unsigned integer type (that is, {{c/core|unsigned char}}, {{c/core|unsigned short}}, {{c/core|unsigned int}}, {{c/core|unsigned long}}, {{c/core|unsigned long long}}, or an extended unsigned integer type)}}.
  
=== Return value ===
+
===Parameters===
Zero if {{tt|x}} is zero; otherwise, the largest integral power of two that is not greater than {{tt|x}}.
+
{{par begin}}
 +
{{par|x|unsigned integer value}}
 +
{{par end}}
  
=== Example ===
+
===Return value===
 +
Zero if {{c|x}} is zero; otherwise, the largest integral power of two that is not greater than {{c|x}}.
 +
 
 +
===Notes===
 +
{{petty|Prior to {{stddoc|P1956R1}}, the proposed name for this function template was {{tt|floor2}}.}}
 +
 
 +
{{feature test macro|__cpp_lib_int_pow2|std=C++20|value=202002L|[[cpp/numeric#Bit manipulation (since C++20)|Integral power-of-2 operations]]}}
 +
 
 +
===Possible implementation===
 +
{{eq fun
 +
|1=
 +
template<std::unsigned_integral T>
 +
    requires !std::same_as<T, bool> && !std::same_as<T, char> &&
 +
            !std::same_as<T, char8_t> && !std::same_as<T, char16_t> &&
 +
            !std::same_as<T, char32_t> && !std::same_as<T, wchar_t>
 +
constexpr T bit_floor(T x) noexcept
 +
{
 +
    if (x != 0)
 +
        return T{1} << (std::bit_width(x) - 1);
 +
    return 0;
 +
}
 +
}}
 +
 
 +
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
 
#include <bit>
 
#include <bit>
 
#include <bitset>
 
#include <bitset>
 
#include <iostream>
 
#include <iostream>
  
auto main() -> int
+
int main()
 
{
 
{
 
     using bin = std::bitset<8>;
 
     using bin = std::bitset<8>;
 
+
     for (unsigned x{}; x != 012; ++x)
     for (unsigned x = 0; x != 10; ++x)
+
         std::cout << "bit_floor( " << bin(x) << " ) = "
    {
+
                  << bin(std::bit_floor(x)) << '\n';
        auto const z = std::bit_floor(x); // `floor2` before P1956R1
+
 
+
         std::cout << "bit_floor(" << bin(x) << ") = " << bin(z) << '\n';
+
    }
+
 
}
 
}
| output=
+
|output=
bit_floor(00000000) = 00000000
+
bit_floor( 00000000 ) = 00000000
bit_floor(00000001) = 00000001
+
bit_floor( 00000001 ) = 00000001
bit_floor(00000010) = 00000010
+
bit_floor( 00000010 ) = 00000010
bit_floor(00000011) = 00000010
+
bit_floor( 00000011 ) = 00000010
bit_floor(00000100) = 00000100
+
bit_floor( 00000100 ) = 00000100
bit_floor(00000101) = 00000100
+
bit_floor( 00000101 ) = 00000100
bit_floor(00000110) = 00000100
+
bit_floor( 00000110 ) = 00000100
bit_floor(00000111) = 00000100
+
bit_floor( 00000111 ) = 00000100
bit_floor(00001000) = 00001000
+
bit_floor( 00001000 ) = 00001000
bit_floor(00001001) = 00001000
+
bit_floor( 00001001 ) = 00001000
 
}}
 
}}
  
{{langlinks|es|ja|zh}}
+
===See also===
 +
{{dsc begin}}
 +
{{dsc inc|cpp/numeric/dsc bit_ceil}}
 +
{{dsc inc|cpp/numeric/dsc rotr}}
 +
{{dsc inc|cpp/numeric/dsc bit_width}}
 +
{{dsc inc|cpp/numeric/dsc has_single_bit}}
 +
{{dsc end}}
 +
 
 +
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}

Latest revision as of 13:55, 2 November 2024

 
 
 
Defined in header <bit>
template< class T >
constexpr T bit_floor( T x ) noexcept;
(since C++20)

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

This overload participates in overload resolution only if T is an unsigned integer type (that is, unsigned char, unsigned short, unsigned int, unsigned long, unsigned long long, or an extended unsigned integer type).

Contents

[edit] Parameters

x - unsigned integer value

[edit] Return value

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

[edit] Notes

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

Feature-test macro Value Std Feature
__cpp_lib_int_pow2 202002L (C++20) Integral power-of-2 operations

[edit] Possible implementation

template<std::unsigned_integral T>
    requires !std::same_as<T, bool> && !std::same_as<T, char> &&
             !std::same_as<T, char8_t> && !std::same_as<T, char16_t> &&
             !std::same_as<T, char32_t> && !std::same_as<T, wchar_t>
constexpr T bit_floor(T x) noexcept
{
    if (x != 0)
        return T{1} << (std::bit_width(x) - 1);
    return 0;
}

[edit] 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';
}

Output:

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

[edit] See also

(C++20)
finds the smallest integral power of two not less than the given value
(function template) [edit]
(C++20)
computes the result of bitwise right-rotation
(function template) [edit]
(C++20)
finds the smallest number of bits needed to represent the given value
(function template) [edit]
checks if a number is an integral power of 2
(function template) [edit]