52905.fb2
The only problem with binary is that we find it so difficult and make too many errors. There is little point in designing microprocessors to handle binary numbers at high speed and with almost 100% accuracy if we are going to make loads of mistakes putting the numbers in and reading the answers.
From our point of view, binary has two drawbacks: the numbers are too long and secondly they are too tedious. If we have streams and streams of ones and zeros we get bored, we lose our place and do sections twice and miss bits out.
The speed of light in m/s can be written in denary as 29979245910 or in binary as 100011101111001111000010010112. Try writing these numbers on a sheet of paper and we can be sure that the denary number will be found infinitely easier to handle. Incidentally, this binary number is less than half the length that a modern microprocessor can handle several millions of times a second with (almost) total accuracy.
In trying to make a denary number even easier, we tend to split it up into groups and would write or read it as 299 792 459. In this way, we are dealing with bite-sized portions and the 10 different digits ensure that there is enough variety to keep us interested. We can perform a similar trick with binary and split the number into groups of four bits starting from the right-hand end as we do with denary numbers.
1 0001 1101 1110 0111 1000 0100 1011
Already it looks more digestible.
Now, if we take a group of four bits, the lowest possible value is 00002 and the highest is 11112. If these binary numbers are converted to denary, the possibilities range from 0 to 15.
Counting from 0 to 15 will mean 16 different digits and so has a base of 16. What the digits look like really doesn’t matter. Nevertheless, we may as well make it as simple as possible.
The first 10 are easy, we can just use 0123456789 as in denary. For the last six we have decided to use the first six letters of the alphabet: ABCDEF or abcdef.
The hex system starts as:
Hex | Denary |
---|---|
0 | 0 |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
A | 10 |
B | 11 |
C | 12 |
D | 13 |
E | 14 |
F | 15 |
When we run out of digits, we just put a 1 in the second column and reset the first column to zero just as we always do.
So the count will continue:
10 | 16 |
11 | 17 |
12 | 18 |
13 | 19 |
14 | 20 |
15 | 21 |
16 | 22 |
17 | 23 |
18 | 24 |
19 | 25 |
1A | 26 |
1B | 27 |
1C | 28 |
1D | 29 |
1E | 30 |
1F | 31 |
20 | 32 |
… and so on.
It takes a moment or two to get used to the idea of having numbers that include letters but it soon passes. We must be careful to include the base whenever necessary to avoid confusion. The base is usually written as H, though h or 16 would still be acceptable.
‘One eight’ in hex is equal to twenty-four in denary. Notice how I avoided quoting the hex number as eighteen. Eighteen is a denary number and does not exist in hex. If you read it in this manner it reinforces the fact that it is not a denary value. Here are the main options in order of popularity:
16H = 2410
16H = 2410
16h = 2410
16h = 2410
1616 = 2410
The advantages of hex
1 It is very compact. Using a base of 16 means that the number of digits used to represent a given number is usually fewer than in binary or denary.
2 It is easy to convert between hex and binary and fairly easy to go between hex and denary. Remember that the microprocessor only works in binary, all the conversions between hex and binary are carried out in other circuits (Figure 3.1).
Figure 3.1 Hex is a good compromise
The process follows the same pattern as we saw in the denary to binary conversion.
Method
1 Write down the denary number.
2 Divide it by 1610, put the whole number part of the answer underneath and the remainder in the column to the right.
3 Keep going until the number being divided reaches zero.
4 Read the answer from the bottom to top of the remainders column.
REMEMBER TO WRITE THE REMAINDERS IN HEX.
A worked example
Convert the denary number 23 823 to hex
1 Write down the number to be converted
23 823
(OK so far).
2 Divide by 16. You will need a calculator. The answer is 1488.9375. The 1488 can be placed under the number being converted
23 823
1488
but there is the problem of the decimal part. It is 0.9375 and this is actually 0.9375 of 16. Multiply 0.9375 by 16 and the result is 15. Remember that this 15 needs to be written as a hex number – in this case F. When completed, this step looks like:
23 823
1488 F
3 Repeat the process by dividing the 1488 by 16 to give 93.0 There is no remainder so we can just enter the result as 93 with a zero in the remainder column.
23 823
1488 F
93 0
4 And once again, 93 divided by 16 is 5.8125. We enter the 5 under the 93 and then multiply the 0.8125 by 16 to give 13 or D in hex
23 823
1488 F
93 0
5 D
5 This one is easy. Divide the 5 by 16 to get 0.3125. The answer has now reached zero and 0.3125×16=5. Enter the values in the normal columns to give:
23 823
1488 F = 5D0F
93 0 ↑
5 D ↑
0 5
6 Read the hex number from the bottom upwards: 5D0FH (remember that the ‘H’ just means a hex number).
Example
Convert 44 25610 into hex
44 256
2766 0 = ACE0H
172 E ↑
10 C
0 A
A further example
Convert 540 70910 to hex
540 709
33 794 5 = 84025H
2112 2 ↑
132 0
8 4
0 8
So 540 70910=84025H but, especially when the hex number does not contain any letters, be careful to include the base of the numbers otherwise life can become really confusing.
To do this, we can use a similar method to the one we used to change binary to denary.
Example
Convert A40E5H to denary
1 Each column increases by 16 times as we move towards the righthand side so the column values are:
164 | 163 | 162 | 161 | 160 |
---|---|---|---|---|
65536 | 4096 | 256 | 16 | 1 |
2 Simply enter the hex number using the columns
65536 | 4096 | 256 | 16 | 1 |
---|---|---|---|---|
A | 4 | 0 | E | 5 |
3 Use your calculator to find the denary value of each column
65536 | 4096 | 256 | 16 | 1 |
---|---|---|---|---|
A | 4 | 0 | E | 5 |
655360 | 16384 | 0 | 224 | 5 |
The left-hand column has a hex value of 1010 (A=10) so the column value is 65536×10=655360. The next column is 4×4096=16384. The next column value is zero (256×0). The fourth column has a total value of 16×14=224 (E = 14). The last column is easy. It is just 1×5=5 no calculator needed!
4 Add up all the denary values:
655 360 + 16 384 + 0 + 224 + 5 = 671 97310
Method
1 Write down the column values using a calculator. Starting on with 160 (=1) on the right-hand side and increasing by 16 times in each column towards the left.
2 Enter the hex numbers in the appropriate column, converting them into denary numbers as necessary. This means, for example, that we should write 10 to replace an ‘A’ in the original number.
3 Multiply these denary numbers by the number at the column header to provide a column total.
4 Add all the column totals to obtain the denary equivalent.
Another example
Convert 4BF0H to denary
163 | 162 | 161 | 160 | column values |
---|---|---|---|---|
4096 | 256 | 16 | 1 | column values |
4 | 11 | 15 | 0 | hex values |
16 384 | 2816 | 240 | 0 | denary column totals |
Total = 16 384 + 2816 + 240 + 0 = 19 44010
This is very easy. Four binary bits can have minimum and maximum values of 00002 up to 11112. Converting this into denary by putting in the column headers of: 8, 4, 2 and 1 results in a minimum value of 0 and a maximum value of 1510. Doesn’t this fit into hex perfectly!
This means that any group of four bits can be translated directly into a single hex digit. Just put 8, 4, 2 and 1 over the group of bits and add up the values wherever a 1 appears in the binary group.
Example
Convert 1000000101010112 to hex
Step 1 Starting from the right-hand end, chop the binary number into groups of four.
100/ 0000/ 1010/ 1011/
Step 2 Treat each group of four bits as a separate entity. The right-hand group is 1011 so this will convert to:
8 | 4 | 2 | 1 | column headers |
---|---|---|---|---|
1 | 0 | 1 | 1 | binary number |
8 | 0 | 2 | 1 | column values |
The total will then be 8 + 0 + 2 + 1 = 1110 or in hex, B. The right-hand side binary group can now be replaced by the hex value B.
100/ | 0000/ | 1010/ | 1011/ |
---|---|---|---|
B |
Step 3 The second group can be treated in the same manner. The bits are 1010 and by comparing them with the 8, 4, 2, 1 header values this means the total value is (8×1)+(4×0)+(2×1)+(1×1) = 8 + 0 + 2 + 0 = 1010 or in hex, A.
We have now completed two of the groups.
100/ | 0000/ | 1010/ | 1011/ |
---|---|---|---|
A | B |
Step 4 The next group consists of all zeros so we can go straight to an answer of zero. The result so far will be:
100/ | 0000/ | 1010/ | 1011/ |
---|---|---|---|
0 | A | B |
Step 5 The last group is incomplete so only the column headings of 4, 2, and 1 are used. In this case, the 4 is counted but the 2 and the 1 are ignored because of the zeros. This gives a final result of:
100/ | 0000/ | 1010/ | 1011/ |
---|---|---|---|
4 | 0 | A | B |
So, 1000000101010112 = 40ABH.
Having chopped up the binary number into groups of four the process is the same regardless of the length of the number. Always remember to start chopping from the right-hand side.
Example
Convert the number 11000111110012 to hex
Split it into groups of four starting from the right-hand side
1/ 1000/ 1111/ 1001/
Add column headers of 8 4 2 1 to each group
1 | 8421 | 8421 | 8421 | column headings |
---|---|---|---|---|
1/ | 1000/ | 1111/ | 1001 | binary number |
1 | 8 | 8421 | 81 | column values |
1 | 8 | 15 | 9 | group value in denary |
Now just convert group values to hex as necessary. In this example only the second group 15, will need changing to F. Final result is 11000111110012=18F9H.
This is just the reverse of the last process. Simply take each hex number and express it as a four bit binary number.
As we saw in the last section, a four-bit number has column header values of 8, 4, 2 and 1, so conversion is just a matter of using these values to build up the required value. All columns used are given a value of 1 in binary and all unused columns are left as zero. When you are converting small numbers like 3H we must remember to add zeros on the left-hand side to make sure that each hex digit becomes a group of four bits.
Imagine that we would like to convert 5H to binary. Looking at the column header values of 8, 4, 2 and 1, how can we make the value 5?
The answer is to add a 4 and a 1. Taking each column in turn: we do not need to use an 8 so the first column is a 0. We do want a 4 so this is selected by putting a 1 in this column, no 2 so make this 0 and finally put a 1 in the last column to select the value of 1. The 5H is converted to 01012. All values between 0 and FH are converted in a similar way.
Example
Convert 2F6CH to binary
Step 1 Write the whole hex number out with enough space to be able to put the binary figures underneath
2 F 6 C
Step 2 Put the column header values below each hex digit
2 | F | 6 | C |
---|---|---|---|
8421 | 8421 | 8421 | 8421 |
Step 3 The hex C is 1210 that can be made of 8+4 so we put a binary 1 in the 8 and the 4 columns. The four-bit number is now 11002
2 | F | 6 | C |
---|---|---|---|
8421 | 8421 | 8421 | 8421 |
1100 |
Step 4 Now do the same for the next column. The hex number is 6, which is made of 4+2, which are the middle two columns. This will result in the binary group 01102
2 | F | 6 | C |
---|---|---|---|
8421 | 8421 | 8421 | 8421 |
0110 | 1100 |
Step 5 Since 8+4+2+1 = 15, the hex F will become 11112
2 | F | 6 | C |
---|---|---|---|
8421 | 8421 | 8421 | 8421 |
1111 | 0110 | 1100 |
Step 6 Finally, the last digit is 2 and since this corresponds to the value of the second column it will be written as 00102
2 | F | 6 | C |
---|---|---|---|
8421 | 8421 | 8421 | 8421 |
0010 | 1111 | 0110 | 1100 |
The final result is 2F6CH = 00101111011011002. But do we include the two leading zeros? There are two answers, ‘yes’ and ‘no’ but that’s not very helpful. We need to ask another question: why did we do the conversion? were we doing math or microprocessors? If we were working on a microprocessor system then the resulting 16 bits would represent 16 voltages being carried on 16 wires. As the numbers change, all the wires must be able to switch between 0 V and 3.3 V for binary levels 0 and 1. This means, of course, that all 16 wires must present so we should include the binary levels on all of them.
If the conversion was purely mathematical, then since leading (lefthand end) zeros have no mathematical value there is no point in including them in the answer.
Method
1 Write down the hex number but make it well spaced.
2 Using the column header values of 8, 4, 2 and 1, convert each hex number to a four bit binary number.
3 Add leading zeros to ensure that every hex digit is represented by four bits.
Example
Convert 1E08BH to binary
Step 1
1 | E | 0 | 8 | B |
---|---|---|---|---|
8421 | 8421 | 8421 | 8421 | 8421 |
Step 2
0001 1110 0000 1000 1011
So, 1E08BH = 000111100000100010112.
Using stepping stones
It is fairly easy to convert binary to hex and hex to binary. I find it much easier to multiply and divide by 2 rather than by 16, so when faced with changing hex into denary and denary into hex I often change them into binary first. It is a longer route but at least I can do it without my calculator (see Figure 3.2).
Figure 3.2 A longer route may prove easier
Octal is another number system which has no advantages over hex but is still met from time to time. Only a brief look will be offered here just to make sure that we have at least mentioned it.
In hex, we used binary bits in groups of four because 11112 adds up to 15 which is the value of the highest digit (F) in hex. In octal, we use groups of three bits. The highest value is now 1112 which is 7. Octal therefore has eight digits and counts from 0 to 7. The count proceeds:
0
1
2
3
4
5
6
7
There is no 8th digit so reset the count to 0 and put a 1 in the next column.
10
11
12
13
14
15
16
17
Now go straight to 20
20
etc.
No letters are involved and it is often not recognized as octal until we realize that none of the numbers involve the digits 8 or 9.
Conversions follow the same patterns as we have seen for hex.
Octal to denary: the column heading values are 84, 83, 82, 81, 80.
Denary to octal: divide by 8 and write down the remainder then read remainders from the bottom upwards. Use the subscript 8 to indicate an octal number, e.g. 6410=1008.
Octal to binary: write each octal digit down as a three digit binary group.
Binary to octal: start from the right-hand side and chop the binary numbers into groups of three, then evaluate each group.
I think that is enough for octal. It’s (fairly) unlikely you will meet it again so we can say ‘goodbye Octal’.
In each case, choose the best option.
1 Which of these represents the largest number?
(a) 10008
(b) 100010
(c) 10002
(d) 1000H
2 The number CD02H is equal to:
(a) 5248210
(b) 5422810
(c) 5632210
(d) 5284210
3 The base of a number system is:
(a) always the same as the highest digit used in the system.
(b) usually +5 or +3.3.
(c) equal to the number of different digits used in the system.
(d) one less than the highest single digit number in the system.
4 Which of these numbers is the same as 101101110102:
(a) 164610
(b) 5BA16
(c) AB5H
(d) B72h
5 The number of digits in a denary number is often:
(a) more than the number of digits in the equivalent binary number.
(b) less than or equal to the number of digits in the equivalent hex number.
(c) more than the number of digits in the equivalent hex number.
(d) more than the number of digits in the equivalent decimal number.