Most humans use the decimal system, which consists of ten digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), because humans have ten fingers. The computer does not have fingers, so it prefers other number systems instead. Here they are….
BINARY
Look at these powers of 2:
2⁰ = 1
2¹ = 2
2² = 4
2³ = 8
2⁴ = 16
2⁵ = 32
2⁶ = 64
Now try an experiment: pick your favorite positive integer, and try to write it as a sum of powers of 2. For example, suppose you pick 45. You can write it as 32+8+4+1. Suppose you pick 74; you can write it as 64+8+2. Suppose you pick 77; you can write it as 64+8+4+1. Every positive integer can be written as a sum of powers of 2.
Let’s put those examples in a table:
ORIGINAL WRITTEN AS A SUM DOES THE SUM CONTAIN... NUMBER OF POWERS OF 2 64? 32? 16? 8? 4? 2? 1? 45 32+8+4+1 no yes no yes yes no yes 74 64+8+2 yes no no yes no yes no 77 64+8+4+1 yes no no yes yes no yes
To write those numbers in the binary system, replace “no” by 0 and “yes” by 1:
DECIMAL SYSTEM BINARY SYSTEM 45 0101101 (or simply 101101) 74 1001010 77 1001101
The decimal system uses the digits 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 and uses these columns:
thousands hundreds tens units
For example, the decimal number 7105 means “7 thousands + 1 hundred + 0 tens + 5 units”.
The binary system uses only the digits 0 and 1, and uses these columns:
sixty-fours thirty-twos sixteens eights fours twos units
For example, the binary number 1001101 means “1 sixty-four + 0 thirty-twos + 0 sixteens + 1 eight + 1 four + 0 twos + 1 unit”; in other words, it means seventy-seven.
In elementary school, you were taught how to do arithmetic in the decimal system. You had to memorize the addition and multiplication tables:
DECIMAL ADDITION DECIMAL MULTIPLICATION
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0 0 0 0 0
1 1 2 3 4 5 6 7 8 9 10 1 0 1 2 3 4 5 6 7 8 9
2 2 3 4 5 6 7 8 9 10 11 2 0 2 4 6 8 10 12 14 16 18
3 3 4 5 6 7 8 9 10 11 12 3 0 3 6 9 12 15 18 21 24 27
4 4 5 6 7 8 9 10 11 12 13 4 0 4 8 12 16 20 24 28 32 36
5 5 6 7 8 9 10 11 12 13 14 5 0 5 10 15 20 25 30 35 40 45
6 6 7 8 9 10 11 12 13 14 15 6 0 6 12 18 24 30 36 42 48 54
7 7 8 9 10 11 12 13 14 15 16 7 0 7 14 21 28 35 42 49 56 63
8 8 9 10 11 12 13 14 15 16 17 8 0 8 16 24 32 40 48 56 64 72
9 9 10 11 12 13 14 15 16 17 18 9 0 9 18 27 36 45 54 63 72 81
In the binary system, the only digits are 0 and 1, so the tables are briefer:
BINARY ADDITION BINARY MULTIPLICATION
0 1 0 1
0 0 1 0 0 0
1 1 10 <-because two is written 1 0 1
"10" in binary
If society had adopted the binary system instead of the decimal system, you would have been spared many hours of memorization!
Usually, when you ask the computer to perform a computation, it converts your numbers from the decimal system to the binary system, performs the computation by using the binary addition and multiplication tables, and then converts the answer from the binary system to the decimal system, so you can read it. For example, if you ask the computer to print 45+74, it will do this:
45 converted to binary is 101101
+74 converted to binary is +1001010
1110111 converted to decimal is 119
^
because 1+1=10
The conversion from decimal to binary and then back to decimal is slow. But the computation itself (in this case, addition) is quick, since the binary addition table is so simple. The only times the computer must convert is during input (decimal to binary) and output (binary to decimal). The rest of the execution is performed quickly, entirely in binary.
You know fractions can be written in the decimal system, by using these columns:
units point tenths hundredths thousandths
For example, 1 5/8 can be written as 1.625, which means “1 unit + 6 tenths + 2 hundredths + 5 thousandths”.
To write fractions in the binary system, use these columns instead:
units point halves fourths eighths
For example, 1 5/8 is written in binary as 1.101, which means “1 unit + 1 half + 0 fourths + 1 eighth”.
You know 1/3 is written in the decimal system as 0.3333333…, which unfortunately never terminates. In the binary system, the situation is no better: 1/3 is written as 0.010101…. Since the computer stores only a finite number of digits, it cannot store 1/3 accurately—it stores only an approximation.
A more distressing example is 1/5. In the decimal system, it’s .2, but in the binary system it’s .0011001100110011…. So the computer can’t handle 1/5 accurately, even though a human can.
Suppose you enter this on your ZX-81 computer:
PRINT "MY FAVORITE NUMBER IS ";4.001-4
The computer will try to convert 4.001 to binary. Unfortunately, it can’t be converted exactly; the computer’s binary approximation of it is slightly too small. The computer’s final answer to 4.001-4 is therefore slightly less than the correct answer. Instead of printing MY FAVORITE NUMBER IS .001, the computer will print MY FAVOURATE NUMBER IS .0010000002 . Other computers come up with numbers like .000999987 .
To test your ZX-81’s accuracy, try 4.0001-4, and 4.00001-4, and 4.000001-4 etc. you might be surprised at its answers.