Menu

Binary Shifts

Shift Overflow and Precision

A binary shift keeps the result to 8 bits, so any bit pushed past either end is dropped. Whether those bits are 0s or 1s decides whether the remaining 8 bits hold the true value.

Left shifts and overflow

A left shift is correct only when every bit lost off the left-hand end is a 0, because a 0 adds no value. If a 1 is lost, the true result is too big to store in 8 bits: this is overflow, and the 8 bits left behind are not the real value. That is not a computer fault, just a result too big for 8 bits.

Shifting 01101010 (106) two places left drops a 0 and a 1, leaving 10101000 (168). The true answer is 424 (106 × 4), too big for 8 bits, so 168 is wrong. By contrast, 00110110 (54) shifted one place left drops a 0 and gives 01101100 (108), exactly 54 × 2.

Right shifts and precision

A right shift is exact only when every bit lost off the right-hand end is a 0. If a 1 falls off the right, part of the value goes with it, so the result is rounded down and precision is lost. Shifting 10100000 (160) four places right drops only 0s and gives 00001010 (10), exactly 160 ÷ 16. Shifting 10110111 (183) two places right drops 11 and gives 00101101 (45); the exact answer, 45.75, has been rounded down.

Showing the effect in denary

Convert the original and the shifted pattern to denary and compare them: if the new value is not the old one multiplied or divided by 2, 4 or 8, then 1s were lost.

StartShiftResultBits lostEffect
00110110 (54)1 left01101100 (108)0Correct, ×2
01101010 (106)2 left10101000 (168)0 and 1Overflow (true 424)
10100000 (160)4 right00001010 (10)0000Exact, ÷16
10110111 (183)2 right00101101 (45)11Rounded down

The first row loses nothing; the second is out by 256, and the last by less than 1.

Practise this