As discussed in Is right shift undefined behavior if the count is larger than the width of the type?, shifting a value is undefined if the number of bit shifts exceeds the effective operand size.
Thus, in the following, the value of bar
is undefined:
uint32_t foo = 123;
uint32_t bar = (foo >> 33);
Are such shift operations well defined for std::bitset
? As in:
std::bitset<32> foo(123);
std::bitset<32> bar(foo >> 33);
And in which official document can I find such information?
The case is not explicitly stated on cppreference (https://en.cppreference.com/w/cpp/utility/bitset/operator_ltltgtgt).
The result is defined as 0 by the standard, [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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With