Binary Shifts
Shifting every bit in a binary number sideways changes the number's value in a very predictable way: each place moved to the left doubles the value, and each place moved to the right halves it. The multiplier or divisor is a power of 2, set by the number of places you move.
| Places moved | Left shift | Right shift |
|---|---|---|
| 1 | ×2 | ÷2 |
| 2 | ×4 | ÷4 |
| 3 | ×8 | ÷8 |
In an 8-bit number the columns are worth 128, 64, 32, 16, 8, 4, 2 and 1, so every column is worth twice the column immediately to its right. Move all the bits one column left and each bit now sits in a column worth double, which doubles the whole number. Move them one column right and each bit drops into a column worth half as much, so the number halves.
Take 00010101, which is 16 + 4 + 1 = 21. To multiply by 8, shift it left 3 places. The bits land in 10101000, which is 128 + 32 + 8 = 168, and 21 × 8 = 168, so the shift has done the multiplication. Going the other way, 01011000 is 88; shift it right 3 places and you get 00001011, which is 11, and 88 ÷ 8 = 11.
This is why a binary shift is a quick way to multiply or divide by a power of two: one shift of the right number of places does the whole calculation, instead of working it out the long way.
OCR GCSE Computer Science · Binary Shifts