std::shr - cppreference.com
Namespaces
Variants

std::shr

From cppreference.com
 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
Integer comparison functions
(C++20)(C++20)(C++20)    
(C++20)
Swap and type operations
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
Common vocabulary types
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
Defined in header <bit>
template< class T, class S >
constexpr T shr( T x, S s ) noexcept;
(since C++29)

Returns ⌊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

⌊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