Part I: Representations are spells

Chapter 02

#Numbers: bigger than you think

How do you make a number bigger than your fingers?

It's no secret that programming is based on numbers. Two of them, 0 and 1. It may come as a surprise that one of those numbers is much newer than the other.

Let's go back, way back, to the first number system our cave-dwelling ancestors ever used: our fingers.

https://en.wikipedia.org/wiki/Tally_marks Hold up your hands and you can count to 10. But what if you want to add a number to your cave painting? Well, you make tally marks: IIII. https://en.wikipedia.org/wiki/Tally_marks

So the first number we used was 1, or the tally mark: I. But tally marks get cumbersome quickly. So someone invented a new symbol to mark 5 tallies: five-bar gate tally mark

And so it stayed for quite a while. Until a Roman (or probably someone else, and the Romans just stole the idea) had the idea to add symbols for other numbers: V for 5, X for 10, and later L, C, D and M for 50, 100, 500 and 1000.

The clever part was not the new symbols. It was that you could write a calculation into the number. 4 is written as IV: V minus I. 6 is VI: V plus I. A smaller mark before a larger one means subtract. After it means add.

https://en.wikipedia.org/wiki/Roman_numerals So what does the Roman numeral MMXXI mean? Add it up: 1000 + 1000 + 10 + 10 + 1 is 2021. Easy enough. Now try MCMXCVIII. That's 1000, plus 1000 minus 100, plus 100 minus 10, plus 5, plus 3. It's 1998, and you had to do sums just to read it. https://en.wikipedia.org/wiki/Roman_numerals

Better than tally marks, but it gets unwieldy fast. And notice: we've come from the cave paintings, at least 35,000 years ago, all the way to the Romans, and we still have to do without 0.

#Stealing the abacus

https://en.wikipedia.org/wiki/Abacus Meanwhile, merchants were counting on the abacus. Its origins are unknown, and it may have been invented more than once. The Romans probably stole it from the Greeks, who may have gotten it from the Egyptians or Babylonians, who probably got it from the Sumerians. https://en.wikipedia.org/wiki/Abacus

By now, you may have noticed a pattern: Everyone is stealing from everyone else.

That's how good ideas travel: they get copied, renamed, improved and eventually taught to children as if they had always existed.

A Chinese abacus with two beads above the bar and five below in each column

This is a Chinese version, still in use in places today. Each column holds one position in a number. The upper beads count 5, the lower beads count 1. (An observant wizard will notice there are more beads than you need to show 0 to 9. The spares hold a column's overflow for a moment in the middle of a calculation, before you carry it.)

The important trick is that each column is worth ten times the column to its right. The same bead means 1, or 10, or 100, depending only on where it sits. The calculation has moved out of the symbols and into their position.

#Nothing, at last

https://en.wikipedia.org/wiki/Hindu%E2%80%93Arabic_numeral_system Our own numerals work exactly like the abacus. The rightmost column is multiplied by 1, the next by 10, then 100, then 1000 and so on. They are usually called Arabic numerals, although the Arabs got them from Indian mathematicians, who had been using them for a few hundred years already. (See the wizard's first rule.) https://en.wikipedia.org/wiki/Hindu%E2%80%93Arabic_numeral_system

And here, for the first time in our story, we need the number 0. You can write 11, but how do you write 'one ten and no ones'? On an abacus you just leave the column empty. On paper, you need a symbol for the empty column. Zero fills that hole.

https://en.wikipedia.org/wiki/0 https://en.wikipedia.org/wiki/Positional_notation The idea of zero is older than this, but its status was shaky. Mathematicians asked how a number, or anything, could be nothing. Positional notation settled the argument by making zero indispensable, first as a placeholder and then as a number in its own right. https://en.wikipedia.org/wiki/0 https://en.wikipedia.org/wiki/Positional_notation

#Numbers that turn themselves

Positional notation also makes numbers mechanical. Picture a simple click counter. Each position is a wheel marked 0 to 9. Each click turns the rightmost wheel one step. When it rolls over from 9 back to 0, a single pin nudges the wheel on its left forward by one. That is the carry: ten on the right becomes one on the left. The rule was already written into the numbers. The machine only had to follow it.

The number wheels of a Roth calculating machine

Blaise Pascal Mechanical calculation Blaise Pascal built one of the first usable mechanical calculators this way in the 1640s. It could add and subtract, and with repeated motions also multiply and divide. It's not a computer yet, but we've come a long way from scratch marks. https://en.wikipedia.org/wiki/Blaise_Pascal

https://en.wikipedia.org/wiki/Antikythera_mechanism https://www.nature.com/articles/444534a (A Greek wizard had built something astonishing much earlier. The Antikythera mechanism, from around 100 BC, used gears to predict astronomical positions and eclipses, long before zero or positional notation reached Europe. Nothing of similar complexity is known until the Renaissance. Some magic gets lost.) https://en.wikipedia.org/wiki/Antikythera_mechanism https://www.nature.com/articles/444534a

#Bigger than you think

https://en.wikipedia.org/wiki/Shannon_number We can now write 1,000,000 and read it without blinking. But science makes up bigger numbers faster than we can write them down. The number of atoms in the observable universe is roughly a 1 followed by 80 zeros. The number of possible games of chess is estimated at around a 1 followed by 120 zeros. https://en.wikipedia.org/wiki/Shannon_number

https://en.wikipedia.org/wiki/Scientific_notation Writing all those zeros is no longer practical. So enter scientific notation, which adds yet another calculation to the notation: the exponent. https://en.wikipedia.org/wiki/Scientific_notation

$$ 10^{120} $$

Exponents also show exactly what positional notation was doing all along:

$$ \times 10^{2} $$$$ \times 10^{1} $$$$ \times 10^{0} $$
972

So the number 972 is equal to $$ 9 \times 10^2 + 7 \times 10^1 + 2 \times 10^0 $$

#Down to two

So what about our original two numbers, 1 and 0? Once you see that the magic is in the positions, it turns out that nothing forces you to use ten numerals per position.

https://en.wikipedia.org/wiki/Hexadecimal Use 16 and you get hexadecimal, with A to F standing in for 10 to 15. The 0x prefix just says 'this is hexadecimal': https://en.wikipedia.org/wiki/Hexadecimal

$$ 0x1F = 1 \times 16 + 15 = 31 $$

https://en.wikipedia.org/wiki/Octal Use 8 and you get octal: https://en.wikipedia.org/wiki/Octal

$$ 0o17 = 1 \times 8 + 7 = 15 $$

https://en.wikipedia.org/wiki/Binary_number Or reduce it to just 1 and 0, and you get binary. Each column is now worth twice the one to its right: https://en.wikipedia.org/wiki/Binary_number

$$ \times 2^{2} $$$$ \times 2^{1} $$$$ \times 2^{0} $$
101

So the binary number 101 is

$$ 1 \times 2^2 + 0 \times 2^1 + 1 \times 2^0 = 4 + 0 + 1 = 5 $$

Binary is everywhere in programming, so you'll meet the powers of 2 often. Here are the first few:

$2^{10}$$2^9$$2^8$$2^7$$2^6$$2^5$$2^4$$2^3$$2^2$$2^1$$2^0$
10245122561286432168421

Two symbols are the fewest a positional system can have, and they are all a machine needs: on or off, a hole or no hole, a pin up or a pin down. That is why computers count in binary.

At every step, the numbers themselves hardly changed. The notation did. And each new notation did some of the thinking for us, until the thinking was simple enough for a machine.