ZX 81 - BASIC Programming

Sinclair ZX Spectrum
Chapter 24.1 - Counting on your fingers
The next chapter digs inside the computer a bit, but before we look at that it would be as well to describe how computers count: they do it using the binary system, which means that they have no fingers - they are all thumbs.

Most European languages count using a more or less regular pattern of tens - in English, for example, although it starts off a bit erratically, it soon settles down into regular groups:

twenty, twenty one, twenty two,...,twenty nine

thirty, thirty one, thirty two,...,thirty nine

forty, forty one, forty two, ...,forty nine

& so on, & this is made even more systematic with the Arabic numerals that we use. However, the only reason for using ten is that we happen to have ten fingers & thumbs.

Now suppose Martians have three extra fingers on each hand (in so far as one can call them fingers): so instead of using our decimal system, with ten as its base, they use a hexadecimal (or hex, for short) system, based on sixteen. They need six extra hex digits in addition to the ten that we use, & they happen to write them as A, B, C, D, E & F. And what comes after F? Just as we, with ten fingers, write 10 for ten, so they, with sixteen, write 10 for sixteen. Their number system starts off:

Hex English    
0 nought    
1 one    
2 two    
:    :    
:    :    
9 nine    
just as ours does, but then it carries on
A ten    
B eleven    
C twelve    
D thirteen    
E fourteen    
F fifteen    
10 sixteen    
11 seventeen    
 :    :    
 :    :    
19 twenty five    
1A twenty six    
1B twenty seven    
 :    :    
 :    :    
1F thirty one    
20 thirty two    
21 thirty three    
 :    :    
 :    :    
9E a hundred & fifty eight    
9F a hundred & fifty nine    
A0 a hundred & sixty    
A1 a hundred & sixty one    
 :    :    
 :    :    
FE two hundred & fifty four    
FF two hundred & fifty five    
100 two hundred & fifty six    

If you are using hex notation & you want to make the fact quite plain, then write 'h' at the end of the number, & say 'hex'. For instance, for a hundred & fifty eight, write '9Eh' & say 'nine E hex'.

You will be wondering what all this has to do with computers. In fact, computers behave as though they had only two digits, represented by a low voltage, or off (0), & a high voltage, or on (1). This is called the binary system, & the two binary digits are called bits: so a bit is either 0 or 1.

In the various systems, counting starts off

English Decimal Hexadecimal Binary
nought 0 0 0 or 0000
one 1 1 1 or 0001
two 2 2 10 or 0010
three 3 3 11 or 0011
four 4 4 100 or 0100
five 5 5 101 or 0101
six 6 6 110 or 0110
seven 7 7 111 or 0111
eight 8 8 1000
nine 9 9 1001
ten 10 A 1010
eleven 11 B 1011
twelve 12 C 1100
thirteen 13 D 1101
fourteen 14 E 1110
fiveteen 15 F 1111
sixteen 16 10 10000

The important point is that sixteen is equal to two raised to the fourth power, & this makes converting between hex & binary very easy.

To convert hex to binary, change each hex digit into four bits, using the table above.

To convert binary to hex, divide the binary number into groups of four bits, starting on the right, & then change each group into the corresponding hex digit.

For this reason, although strictly speaking computers use a pure binary system, humans often write the numbers stored inside a computer using hex notation.

The bits inside the computer are mostly grouped into sets of eight, or bytes. A single byte can represent any number from nought to two hundred & fifty five (11111111 binary or FF hex), or alternatively any character in the ZX81 character set. Its value can be written with two hex digits.

Two bytes can be grouped together to make what is technically called a word. A word can be written using sixteen bits of hex digits, & represents a number from 0 to (in decimal) 216-1 = 65535.

A byte is always eight bits, but words vary from computer to computer.

Summary

Decimal, hexadecimal & binary systems.

Bits & bytes (don't confuse them) & words.

Sinclair ZX Spectrum

  Previous Page Back Next Page