std::shr
Defined in header <bit> | ||
|---|---|---|
template< class T, class S > constexpr T shr( T x, S s ) noexcept; | (since C++29) |
Returns \(\small{\lfloor x \times 2^{-s} \rfloor}\)⌊x · 2-s⌋ rounded towards negative infinity, and truncated to fit into T.
Parameters
| x | - | value of unsigned integer type |
| s | - | number of positions to shift |
Return value
\(\small{\lfloor x \times 2^{-s} \rfloor}\)⌊x · 2-s⌋ truncated to the result type.
Notes
Unlike the >> operator, std::shr never has undefined behavior. Shifting takes place as if by shifting to the right by a single bit s times, or shifting to the left by a single bit -s times if s is negative, except that -s cannot overflow.
| Feature-test macro | Value | Std | Feature |
|---|---|---|---|
__cpp_lib_bitops | 202606L | (C++29) | Better shifting |
Possible implementation
template<class T, class S> // TODO: constraints
constexpr T shr(T x, S s) noexcept
{
constexpr auto width = S(std::numeric_limits<std::make_unsigned_t<T>>::digits);
if constexpr (std::is_signed_v<S>)
{
if (s < 0)
return s <= -width ? T(0) : x << -s;
}
return s >= width ? T(x < 0 ? -1 : 0) : x >> s;
}
|
Example
#include <bit>
#include <concepts>
#include <cstdint>
#include <limits>
template<std::unsigned_integral T>
constexpr bool is_set(T bits, int bit_num) noexcept
{
#if (__cpp_lib_bitops >= 202606L)
// Never undefined.
return std::shr(bits, bit_num) & 1;
#else
// Requires special cases to guard against overlong
// shifts, otherwise the behavior is undefined.
if (std::numeric_limits<T>::digits < bit_num)
return false;
return (bits >> bit_num) & 1;
#endif
}
static_assert( is_set(0b101U, 0));
static_assert(!is_set(0b101U, 1));
static_assert( is_set(0b101U, 2));
static_assert(!is_set(0b101U, 69)); // well formed
#if (__cpp_lib_bitops >= 202606L)
static_assert(std::shr(std::uint8_t(0b0011), -2) == 0b1100); // negative shift is OK
#endif
int main() {}
See also
|
(C++29) | shifts to the left without the possibility of undefined behavior (function template) |
|
(C++20) | computes the result of bitwise left-rotation (function template) |
|
(C++20) | computes the result of bitwise right-rotation (function template) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/cpp/numeric/shr