What happens when you bit shift beyond the end of a variable?

It does not (necessarily) become zero. The behavior is undefined (C99 §6.5.7, “Bitwise shift operators”):

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.

(C++0x §5.8, “Shift operators”):

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.

The storage of the value being shifted has no effect on any of this.

Leave a Comment