|
|
|
|
|
by Findecanor
848 days ago
|
|
One thing that is still "implementation-defined" in C and C++ is the result of a right shift of a negative integer.
On pretty much all platforms, the >> operator shifts in the sign bit and does not round the result — which makes it equivalent to flooring division by a power of two.
It is consistent with the division operator only when the left-hand value is positive. |
|
More precisely, regarding E1 << E2, it is written (in the April 2023 ISO C draft):
"If E1 has a signed type and nonnegative value, and E1 × 2ᴱ² is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined."
Thus if E1 is negative, or if the result overflows, UB.