When entering numbers into a computer you can still use base 10 if you wish, but another base -- base 16 -- is also available. For reasons which should become clear as you read through this chapter, base 16 (or hexadecimal) is far more suitable for working with computers. Hence it is advisable at this stage to spend some time becoming familiar with this number system.
In base 16 we need 16 different symbols to represent the 16 different 'hexadecimal digits'. For convenience we retain the symbols 0 to 9, and use the letters A to F to represent the values of ten to fifteen.
Another difference between base 10 and base 16 is what happens to a digit or hexadecimal digit when it is shifted one column to the left. Whereas we have seen that in base 10 this multiplies the value of the digit by ten, in base 16 it multiplies the value by sixteen. Hence 10 in hexadecimal represents the value sixteen.
Having two bases in which we can work can lead to confusion. Consider, for example, the number 10; as we have seen this can represent either of the values ten or sixteen depending on whether it is being interpreted as a decimal or hexadecimal number. We need a method of specifying whether a number is decimal or hexadecimal. Normally we do this by prefixing hexadecimal numbers with an ampersand (&), e.g.
&B1
The 'B' has the value 16*11 because it is in the second column to the left, and the '1' represents 1 unit; the number therefore has the decimal value 176 + 1, i.e. 177.
&123
The '1' is in the third column to the left, so it has the value 16*16*1, the '2' has the value 16*2 and the '3' has the value 3. Adding these together produces 256 + 32 + 3, i.e. 291
There is no real need to learn how to convert between hexadecimal and decimal because the computer can do it for you, as shown below.
PRINT &123
The answer, 291, is printed out.
PRINT ~&123
The answer, 7B, is printed out. The number printed will be in hexadecimal notation, but note that the computer doesn't use the symbol '&' when it is printing hexadecimal numbers. In this case it is obvious that the answer is a hexadecimal number but for an answer such as 79 you would need to know which base you requested the computer to use to be able to interpret the result correctly.
The symbol twiddle or, more accurately, ti(de) ~ means 'print in hexadecimal'; thus writing
PRINT~123
will print 123.
0 1 zero one low high clear set off on false true
The circuits are said to be in a 'bistable state', i.e. they are always in one of two possible states. When the digits 0 and 1 are used to refer to these two states they are termed 'binary digits', or 'bits' for brevity.
With a 'nibble', which is four bits, 16 different values can be represented (16 = 2^4). This means that a hexadecimal digit can be represented by a four-bit binary number. The hexadecimal digits and their binary equivalents are shown in the following table:
Decimal | Hexadecimal | Binary |
0 | 0 | 0 0 0 0 |
1 | 1 | 0 0 0 1 |
2 | 2 | 0 0 1 0 |
3 | 3 | 0 0 1 1 |
4 | 4 | 0 1 0 0 |
5 | 5 | 0 1 0 1 |
6 | 6 | 0 1 1 0 |
7 | 7 | 0 1 1 1 |
8 | 8 | 1 0 0 0 |
9 | 9 | 1 0 0 1 |
10 | A | 1 0 1 0 |
11 | B | 1 0 1 1 |
12 | C | 1 1 0 0 |
13 | D | 1 1 0 1 |
14 | E | 1 1 1 0 |
15 | F | 1 1 1 1 |
Any hexadecimal number can be converted into its binary representation by the simple procedure of converting each hexadecimal digit into the corresponding four bits, for example
Thus the binary equivalent of &19 is 00011001, (or leaving out the leading zeros which are irrelevant, 11001).
We have seen already that each hexadecimal digit requires four bits to specify it. A byte, since it contains eight bits, can therefore represent any hexadecimal number between 0 and &FF.
The bits in a byte are usually numbered for convenience, as follows:
Bit 0 is often referred to as the 'low-order bit' or 'least-significant bit', and bit 7 as the 'high-order bit' or most-significant bit'.
An address can be one or two bytes long. This means that addresses can cover the range 0 to &FFFF. For a detailed look at which part of the memory each address corresponds to see the memory maps in Appendix A.
PRINT ?&FFEE
This prints out the value found at the location specified, which in this case should be the number 108. Any memory location can be examined in this way and all of them will contain a number between 0 and 255.
It is often convenient to look at several memory locations in a row; for example, to list the contents of the 32 memory locations from &70 upwards, type
FOR N = 0 TO 31 : PRINT ?(N+&70); : NEXT N
An alternative way of writing this is
FOR N=0 TO 31 : PRINT N?&70; : NEXT N
This method is tidier than the other and gives identical results; i.e. for each of the values of N between 0 and 31, N is added to the number &70 to give the address of the location whose contents are to be printed out. This should result in the contents of 32 memory locations being listed on the screen.
?&70=7
To verify the change, type
PRINT ?&70
Try setting the contents of this memory location to other numbers. Setting the contents to a number greater than 255 or &FF will result in the number entered modulo 256 being stored there, for example
?&70=600 PRINT ?&70
This will print out
88 (600 MOD 256)
A word of warning: Before you change the contents of any other memory locations be sure that you know what you are doing. Although it is quite safe to look at almost any memory location in the computer, care must be exercised when changing any of them. The example given here uses a specific location which is not used by the computer; if you change any other location you may lose any program you have in memory or confuse the computer to such an extent that it proves necessary to reset it by pressing BREAK to make it accept any further commands.
+5 = 00000101
We then find the complement of this, i.e. change each 0 into a 1 and each 1 into a 0, e.g.
complement of +5 = 1111010
Finally we add one:
11111010 + 1 11111011
This gives us the two's complement representation of -5.
We can now try adding together +5 and -5 to see if they give us 0.
00000101 11111011 (1) 00000000
Ignoring the 1 which has overflowed gives us the result, zero, which we were expecting.
Note that when representing numbers using two's complement notation a single byte can represent any number between -128 and +127. The left-hand bit is 1 if the number is negative and 0 otherwise. Zero is classed as a non-negative number.
The unique number corresponding to each character is given by its ASCII code (American Standard Code for Information Interchange). To find the ASCII code of a given character the ASC function can be used, for example type
PRINT ASC "A"
and the number 65 will be printed out. This means that the character 'A' is represented internally by the number 65. If you try repeating this process for B C D ..... you will notice that there is a certain regularity. The same is true for a b c d ... and the sequence 1 2 3 4 ....
A full table of the ASCII codes used to represent all the characters is given in Appendix A.