Difference between revisions of "cpp/numeric/bit ceil"
From cppreference.com
m (fmt.) |
m (fmt.) |
||
(5 intermediate revisions by 4 users not shown) | |||
Line 6: | Line 6: | ||
}} | }} | ||
− | Calculates the smallest integral power of two that is not smaller than {{ | + | Calculates the smallest integral power of two that is not smaller than {{c|x}}. |
If that value is not representable in {{tt|T}}, the behavior is undefined. Call to this function is permitted in [[cpp/language/constant expression|constant evaluation]] only if the undefined behavior does not occur. | If that value is not representable in {{tt|T}}, the behavior is undefined. Call to this function is permitted in [[cpp/language/constant expression|constant evaluation]] only if the undefined behavior does not occur. | ||
− | {{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)}}. |
− | === Parameters === | + | ===Parameters=== |
{{par begin}} | {{par begin}} | ||
− | {{par | x | | + | {{par|x|value of unsigned integer type}} |
{{par end}} | {{par end}} | ||
− | === Return value === | + | ===Return value=== |
− | The smallest integral power of two that is not smaller than {{ | + | The smallest integral power of two that is not smaller than {{c|x}}. |
− | === Exceptions === | + | ===Exceptions=== |
Throws nothing. | Throws nothing. | ||
+ | |||
+ | ===Notes=== | ||
+ | {{petty|Prior to {{stddoc|P1956R1}}, the proposed name for this function template was {{tt|ceil2}}.}} | ||
+ | |||
+ | {{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=== | ===Possible implementation=== | ||
− | See possible implementations in [https://github.com/gcc-mirror/gcc/blob/62c25d7adb1a5664982449dda0e7f9ca63cf4735/libstdc%2B%2B-v3/include/std/bit#L217-L248 libstdc++ (gcc)] and [https://github.com/llvm- | + | See possible implementations in [https://github.com/gcc-mirror/gcc/blob/62c25d7adb1a5664982449dda0e7f9ca63cf4735/libstdc%2B%2B-v3/include/std/bit#L217-L248 libstdc++ (gcc)] and [https://github.com/llvm/llvm-project/blob/llvmorg-14.0.4/libcxx/include/bit#L304-L321 libc++ (clang)]. |
{{eq fun|1= | {{eq fun|1= | ||
− | template <std::unsigned_integral T> | + | template<std::unsigned_integral T> |
requires !std::same_as<T, bool> && !std::same_as<T, char> && | 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, char8_t> && !std::same_as<T, char16_t> && | ||
− | !std::same_as<T, char32_t> && !std::same_as<T, wchar_t> | + | !std::same_as<T, char32_t> && !std::same_as<T, wchar_t> |
constexpr T bit_ceil(T x) noexcept | constexpr T bit_ceil(T x) noexcept | ||
{ | { | ||
Line 36: | Line 41: | ||
if constexpr (std::same_as<T, decltype(+x)>) | if constexpr (std::same_as<T, decltype(+x)>) | ||
return T(1) << std::bit_width(T(x - 1)); | return T(1) << std::bit_width(T(x - 1)); | ||
− | else { // for types subject to integral promotion | + | else |
+ | { // for types subject to integral promotion | ||
constexpr int offset_for_ub = | constexpr int offset_for_ub = | ||
std::numeric_limits<unsigned>::digits - std::numeric_limits<T>::digits; | std::numeric_limits<unsigned>::digits - std::numeric_limits<T>::digits; | ||
Line 44: | Line 50: | ||
}} | }} | ||
− | === Example === | + | ===Example=== |
{{example | {{example | ||
− | + | |code= | |
− | + | ||
#include <bit> | #include <bit> | ||
#include <bitset> | #include <bitset> | ||
#include <iostream> | #include <iostream> | ||
− | + | int main() | |
{ | { | ||
using bin = std::bitset<8>; | using bin = std::bitset<8>; | ||
− | + | for (auto x{0U}; 0XA != x; ++x) | |
− | for ( | + | std::cout << "bit_ceil( " << bin(x) << " ) = " |
− | + | << bin(std::bit_ceil(x)) << '\n'; | |
− | + | ||
− | + | ||
− | std::cout << "bit_ceil( " << bin(x) << " ) = " << bin( | + | |
− | + | ||
} | } | ||
− | + | |output= | |
bit_ceil( 00000000 ) = 00000001 | bit_ceil( 00000000 ) = 00000001 | ||
bit_ceil( 00000001 ) = 00000001 | bit_ceil( 00000001 ) = 00000001 | ||
Line 76: | Line 77: | ||
}} | }} | ||
− | === See also === | + | ===See also=== |
{{dsc begin}} | {{dsc begin}} | ||
− | {{dsc inc | cpp/numeric/dsc bit_floor}} | + | {{dsc inc|cpp/numeric/dsc bit_floor}} |
{{dsc end}} | {{dsc end}} | ||
{{langlinks|de|es|fr|it|ja|pt|ru|zh}} | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} |
Latest revision as of 13:49, 2 November 2024
Defined in header <bit>
|
||
template< class T > constexpr T bit_ceil( T x ); |
(since C++20) | |
Calculates the smallest integral power of two that is not smaller than x.
If that value is not representable in T
, the behavior is undefined. Call to this function is permitted in constant evaluation only if the undefined behavior does not occur.
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 | - | value of unsigned integer type |
[edit] Return value
The smallest integral power of two that is not smaller than x.
[edit] Exceptions
Throws nothing.
[edit] Notes
Prior to P1956R1, the proposed name for this function template was ceil2
.
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_int_pow2 |
202002L | (C++20) | Integral power-of-2 operations |
[edit] Possible implementation
See possible implementations in libstdc++ (gcc) and libc++ (clang).
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_ceil(T x) noexcept { if (x <= 1u) return T(1); if constexpr (std::same_as<T, decltype(+x)>) return T(1) << std::bit_width(T(x - 1)); else { // for types subject to integral promotion constexpr int offset_for_ub = std::numeric_limits<unsigned>::digits - std::numeric_limits<T>::digits; return T(1u << (std::bit_width(T(x - 1)) + offset_for_ub) >> offset_for_ub); } } |
[edit] Example
Run this code
#include <bit> #include <bitset> #include <iostream> int main() { using bin = std::bitset<8>; for (auto x{0U}; 0XA != x; ++x) std::cout << "bit_ceil( " << bin(x) << " ) = " << bin(std::bit_ceil(x)) << '\n'; }
Output:
bit_ceil( 00000000 ) = 00000001 bit_ceil( 00000001 ) = 00000001 bit_ceil( 00000010 ) = 00000010 bit_ceil( 00000011 ) = 00000100 bit_ceil( 00000100 ) = 00000100 bit_ceil( 00000101 ) = 00001000 bit_ceil( 00000110 ) = 00001000 bit_ceil( 00000111 ) = 00001000 bit_ceil( 00001000 ) = 00001000 bit_ceil( 00001001 ) = 00010000
[edit] See also
(C++20) |
finds the largest integral power of two not greater than the given value (function template) |