移动 std::bitset<N> 是否超过 N 个位置未定义的行为?
Is shifting std::bitset<N> more than N positions undefined behavior?
如 Is right shift undefined behavior if the count is larger than the width of the type? 中所讨论,如果移位的次数超过有效操作数大小,则移位值是未定义的。
因此,在下面,bar
的值是未定义的:
uint32_t foo = 123;
uint32_t bar = (foo >> 33);
是否为 std::bitset
明确定义了此类移位操作?如:
std::bitset<32> foo(123);
std::bitset<32> bar(foo >> 33);
在哪个官方文档中可以找到这样的信息?
cppreference (https://en.cppreference.com/w/cpp/utility/bitset/operator_ltltgtgt) 上没有明确说明这种情况。
结果按标准定义为0,[bitset.members]/9:
bitset& operator>>=(size_t pos) noexcept;
9 Effects: Replaces each bit at position I in *this with a value
determined as follows:
(9.1) If pos >= N - I, the new value is zero;
(9.2) If pos < N - I, the new value is the previous value of the bit
at position I + pos.
如 Is right shift undefined behavior if the count is larger than the width of the type? 中所讨论,如果移位的次数超过有效操作数大小,则移位值是未定义的。
因此,在下面,bar
的值是未定义的:
uint32_t foo = 123;
uint32_t bar = (foo >> 33);
是否为 std::bitset
明确定义了此类移位操作?如:
std::bitset<32> foo(123);
std::bitset<32> bar(foo >> 33);
在哪个官方文档中可以找到这样的信息?
cppreference (https://en.cppreference.com/w/cpp/utility/bitset/operator_ltltgtgt) 上没有明确说明这种情况。
结果按标准定义为0,[bitset.members]/9:
bitset& operator>>=(size_t pos) noexcept;
9 Effects: Replaces each bit at position I in *this with a value determined as follows:
(9.1) If pos >= N - I, the new value is zero;
(9.2) If pos < N - I, the new value is the previous value of the bit at position I + pos.