|Binary and hexadecimal|
|This appendix describes how computers count, using the binary system.
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,
and so on, and 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 and thumbs.
Instead of using the decimal system, with ten as its base, computers use a form of binary called hexadecimal (or hex, for short), based on sixteen. As there are only ten digits available in our number system we need six extra digits to do the counting. So we use A, B, C, D, E and F. And what comes after F? Just as we, with ten fingers, write 10 for ten, so computers write 10 for sixteen. Their number system starts off:
If you are using hex notation and you want to make the fact quite plain, then write 'h' at the end of the number, and say 'hex'. For instance, for one hundred and fifty eight, write '9Eh' and 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), and a high voltage, or on (1). This is called the binary system, and the two binary digits are called bits: so a bit is either 0 or 1.
In the various systems, counting starts off
The important point is that sixteen is equal to two raised to the fourth power, and this makes converting between hex and 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, and 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 and fifty five (11111111 binary or FF hex), or alternatively any character in the ZX Spectrum 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 or four hex digits, and represents a number from 0 to (in decimal) 2l6-l=65535.
A byte is always eight bits, but words vary in length from computer to computer.
The BIN notation in Chapter 14 provides a means of writing numbers in binary on the ZX Spectrum: 'BIN 0' represents nought, 'BIN 1' represents one, 'BIN 10' represents two, and so on.
You can only use 0's and 1 's for this, so the number must be a non negative whole number; for instance you can't write 'BIN-11' for minus three - you must write '-BIN 11' instead. The number must also be no greater than decimal 65535 - i.e. it can't have more than sixteen bits.
ATTR really was binary. If you convert the result from ATTR into binary, you can write it in eight bits.
The first is 1 for flashing, 0 for steady.
The second is 1 for bright, 0 for normal.
The next three are the code for the paper colour, written in binary.
The last three are the code for the ink colour, written in binary.
The colour codes also use binary: each code written in binary can be written in three bits, the first for green, the second for red and the third for blue.
Black has no light at all, so all the bits are 0 (off). Therefore the code for black is 000 in binary, or nought.
The pure colours, green, red and blue have just one bit 1 (on) out of the three. Their codes are 100, 010 and 001 in binary, or four, two and one.
The other colours are mixtures of these, so their codes in binary have two or more bits 1.