Skip to content
Unit 5 of 61 hr learning time

Slide the Whole Row

Shifting slides every bit left or right at once — and because each place is worth twice the last, a slide is the machine's quickest way to multiply and divide by two.

83% of Numbers & Bits

One move left. The bit logic in Unit 4 changed which switches were on. This last tool doesn’t care which are on — it slides the whole row sideways, every bit shuffling one place over together. It’s called shifting, and the lovely thing about it 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. Remember that 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 — 00000101. Slide it left and it becomes 00001010, which is 10:

  10 PRINT 5 * 2
A Spectrum screen showing the number 10 printed below the program PRINT 5 * 2.
5 doubled is 10 — and in bits, 00000101 slid left to 00001010. Spectrum BASIC has no shift word, but multiplying by two does the exact same thing to the switches.

Spectrum BASIC has no “shift” word — but multiplying by two does precisely what a left shift does to the bits, so it’s our stand-in. In assembly there’s a real shift instruction that slides the row in a single, fast step.

Each slide is another doubling

Shift left again and you double again. Slide by three places and you’ve doubled three times over — that’s times eight. A shift left by n multiplies by 2 to the power n: one place ×2, two places ×4, three places ×8:

  10 PRINT 5 * 8
A Spectrum screen showing the number 40 printed below the program PRINT 5 * 8.
Three slides left: 00000101 becomes 00101000 — 5 times eight is 40. Each place the row moves is one more doubling.

Slide right, and it halves

Slide the row the other way — one place right — and every bit drops to a lower place. That halves the number. The catch: the bit on the far right has nowhere to go and falls off the end. So a right shift is a divide by two that throws away any remainder — exactly what whole-number division does. 5 (00000101) slid right is 2 (00000010); the spare 1 is gone:

  10 PRINT INT (5 / 2)
A Spectrum screen showing the number 2 printed below the program PRINT INT (5 / 2).
5 slid right is 2 — 00000101 became 00000010, and the bottom 1 fell off the end. That lost bit is the remainder a right shift discards.

Why the machine loves it

Real multiplication and division are slow work for an old CPU. 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, 16 — sizing a sprite, stepping through a table, splitting a byte into nibbles — 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, the high bits gone for good.
  • A right shift “lost” part of the answer. It did, on purpose — the bottom bit falls off the end, so a right shift is division that discards the remainder. 5 ÷ 2 gives 2, not 2.5.
  • × 2 didn’t match a shift. The two agree only for whole numbers, and only while the result still fits — once bits fall off either end, the shift and the arithmetic part ways.

What you’ve learnt

  • Shifting slides every bit one place at once — the whole row together.
  • Left is ×2 (a 0 fills in at the right); shift by n places to multiply by 2ⁿ.
  • Right is ÷2 (the bottom bit falls off as the discarded remainder).
  • It’s the machine’s fastest way to scale by a power of two — which is why old code is full of it.

What’s next

You can read a byte every way the machine does, and slide its bits to scale it — but every number so far has been a whole one. Real games run on the in-between: half a pixel, two-thirds speed. In Unit 6, the last of the primer, we handle fractions — the two ways to carry one, and why games almost always nailed the point down rather than letting it float.