Arithmetic right shift gives bogus result?

In C++ as in C, shifts are limited to the size (in bits) of the value shifted. For instance, if unsigned int is 32 bits, then a shift of more than 31 is undefined.

In practice, a common result is that the 5 least-significant bits of the shift amount are used and the higher order bits are ignored; this is due to the compiler producing a machine instruction which does exactly that (eg SHR on x86).

In this case, the shift value is 100000 (decimal) which happens to be 11000011010100000 in binary – the lower 5 bits are zero. So, you’re effectively getting a shift by 0. You shouldn’t rely on this however; technically, what you’re seeing is undefined behaviour.

References:

For C, N1570 section 6.5.7:

If the value of the right operand is negative or is greater than or
equal to the width of the promoted left operand, the behavior is
undefined.

For C++, N3690 section 5.8 “[expr.shift]”:

The behavior is undefined if the right operand is negative, or greater
than or equal to the length in bits of the promoted left operand.

N1570 is a draft, nearly identical to the released ISO C11 standard; this clause has been pretty much the same since the 1989 ANSI C standard.

N3690 is a recent draft of the C++ standard; I’m not sure whether it’s the best one to use, but again, this clause hasn’t changed.

Leave a Comment