# Arithmetic bit-shift on a signed integer

Right shift of a negative signed number has implementation-defined behaviour.

If your 8 bits are meant to represent a signed 8 bit value (as you're talking about a "signed 32 bit integer" before switching to 8 bit examples) then you have a negative number. Shifting it right may fill "empty" bits with the original MSB (i.e. perform sign extension) or it may shift in zeroes, depending on platform and/or compiler.

(Implementation-defined behaviour means that the compiler will do something sensible, but in a platform-dependent manner; the compiler documentation is supposed to tell you what.)

A left shift, if the number either starts out negative, or the shift operation would shift a 1 either to or beyond the sign bit, has undefined behaviour (as do most operations on signed values which cause an overflow).

(Undefined behaviour means that anything at all could happen.)

The same operations on *unsigned* values are well-defined in both cases: the "empty" bits will be filled with 0.

Bitwise shift operations are not defined for negative values

for '<<'

6.5.7/4 [...] If E1 has a signed type and nonnegative value, and E1×2

^{E2}is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.

and for '>>'

6.5.7/5 [...] If E1 has a signed type and a negative value, the resulting value is implementation- defined.

It's a waste of time to study the behaviour of these operations on signed numbers on a specific implementation, because you have no guarantee it will work the same way on any other implementation (an implementation is, for example, you compiler on your computer with your specific commad-line parameters).

It might not even work for an older or a newer version of the very same compiler. The compiler might even define those bits as random or undefined. This would mean that the very same code sequence could produce totally different results when used across your sources or even depend on things like assembly optimisation or other register usage. If encapsulated in a function it might not even produce the same result in those bits on two consecutive calls with the same arguments.

**Considering only non-negative values**, the effect of left shifting by 1 (`expression << 1`

) is the same as multpliying the expression by 2 (provided expression * 2 does not overflow) and the effect of right shifting by 1 (`expression >> 1`

) is the same as dividing by 2.

**As of c++20** the bitwise shift operators for signed integers are well defined.

The left shift `a<<b`

is equivalent to `a*2^b`

modulus `2^N`

where `N`

is the number of bits in the resulting type. In particular `1<<31`

is in fact the smallest `int`

value.

The right shift `a>>b`

is equivalent to `a/2^b`

, rounded down (ie. towards negative infinity). So e.g. `-1>>10 == -1`

.

For some more details see https://en.cppreference.com/w/cpp/language/operator_arithmetic .

(for the older standards see the answer by Matthew Slattery)