std::shr
来自cppreference.com
| 在标头 <bit> 定义
|
||
| |
(C++29 起) | |
返回向负无穷方向舍入的 ⌊x · 2-s
⌋,并截断以适配 T 类型。
参数
| x | - | 无符号整数类型的值 |
| s | - | 要移位的位数 |
返回值
截断至结果类型的 ⌊x · 2-s
⌋。
注解
与 >> 运算符不同,std::shr 永远不会导致未定义行为。
移位操作如同逐位向右移动 s 次;若 s 为负数,则如同逐位向左移动 -s 次,但需注意 -s 不会溢出。
| 功能特性测试宏 | 值 | 标准 | 功能特性 |
|---|---|---|---|
__cpp_lib_bitops |
202606L |
(C++29) | 更好的移位 |
可能的实现
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;
}
|
示例
Run this code
#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)
// 永远不是未定义行为。
return std::shr(bits, bit_num) & 1;
#else
// 需要特殊处理以防卫过长的移位,
// 否则行为未定义。
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)); // 良构
#if (__cpp_lib_bitops >= 202606L)
static_assert(std::shr(std::uint8_t(0b0011), -2) == 0b1100); // 负移位 OK
#endif
int main() {}
参阅
(C++29) |
向左移位并避免未定义行为可能性 (函数模板) |
(C++20) |
计算逐位左旋转的结果 (函数模板) |
(C++20) |
计算逐位右旋转的结果 (函数模板) |