Slide the Whole Row
Shifting slides every bit sideways at once, which is the machine's quickest way to multiply and divide by two.
One move left. The bit logic in Unit 1 changed which switches were on. This tool does not care which are on. It slides the whole row sideways, every bit shuffling one place over together.
It is called shifting, and the interesting part is what the sliding does to the number.
Slide left, and the number doubles
Push every bit one place to the left. Each switch moves into the next place up, and a 0 drifts in to fill the gap on the right.
Every place is worth twice the one to its right. So moving every bit up one place makes the whole number worth twice as much. A left shift is a doubling.
Take 5, which is 00000101. Slide it left and it becomes 00001010, which is 10:
SHOW 5 SHIFTLEFT 1
10
The number after SHIFTLEFT is how many places to slide. One place, one doubling.
Each slide is another doubling
Shift left again and you double again. Slide by three places and you have doubled three times over, which is times eight.
A shift left by n multiplies by 2 to the power n. One place is ×2, two places ×4, three places ×8:
SHOW 5 SHIFTLEFT 3
40
Three slides: 00000101 becomes 00101000, and 5 times eight is 40.
Slide right, and it halves
Slide the row the other way and every bit drops to a lower place. That halves the number.
There is a catch. The bit on the far right has nowhere to go, so it falls off the end. A right shift is a divide by two that throws away any remainder, which is exactly what whole-number division does.
SHOW 5 SHIFTRIGHT 1
2
Not 2.5. 00000101 became 00000010, and the bottom 1 fell off. That lost bit is the
remainder.
Why the machine loves it
Real multiplication and division are slow work for an old CPU. Some of these processors have no multiply instruction at all.
Shifting is almost free. Sliding the switches is one of the quickest things the machine can do.
So whenever a value needs scaling by 2, 4, 8 or 16, the machine reaches for a shift instead.
When you see ×2 or ÷2 in old code, picture the whole row of bits sliding one place over.
When it’s wrong, see why
- A left-shifted number came out far too small. Bits slid off the top and were lost. Shift too far and the value overflows its byte.
- A right shift lost part of the answer. It did, on purpose. The bottom bit falls off, so a right shift discards the remainder. 5 ÷ 2 gives 2, not 2.5.
× 2did not match a shift. The two agree only for whole numbers, and only while the result still fits. Once bits fall off either end they part ways.
What you’ve learnt
- Shifting slides every bit one place at once, the whole row together.
- Left is ×2, and a shift by n places multiplies by 2ⁿ.
- Right is ÷2, and the bottom bit falls off as the discarded remainder.
- It is the machine’s fastest way to scale by a power of two, which is why old code is full of it.
What’s next
Every number so far has been a number. In Unit 3 we look at a byte holding a letter, and find the bit tools waiting for us there.