## Architecture 2 - Hexadecimal Numbers

**Vexed by Hex?**

One of my pet hates in examinations is the inevitable question - "Show how the denary number 3750 is stored in a hexadecimal format."

And the answer is...

**IT ISN'T! COMPUTERS CANNOT STORE NUMBERS IN HEXADECIMAL!** They can only store things in Binary!!!

OK, rant over for the moment. What computers can, and do, do, is **display** numbers in a hexadecimal format. In fact you will have seen this format many times already, but probably not appreciated it for what it was.

The introduction of hexadecimal format numbers was a huge innovation in computing science. Prior to hex formats, computers could only display output, and accept input, in a binary format. Entire programs had to be written in binary format, by highly trained mathematicians or logicians, and input by hand in 0-1 equivalents. But as computers became more readily available, the lack of personnel trained to such a high mathematical standard became a bit of a problem. And of course, when transposing or copying large blocks of 1s and 0s, it was too easy to make mistakes.

The eyes and brains of higher-level organisms (like humans) are simply not designed to pick out individual items in large groups of similar objects. Study the animation below -

How many digits the first time? How many, the second?

Human eyes and brains can only register up to about five or six separate items at first glance; after that we have to actually count the items individually, to get an accurate number. Now imagine trying to transpose a 64-digit binary number in a very short space of time. The chances are that you will get it wrong.

**This is why computers display numbers in hex!**

A better method of displaying numbers had to be found, and it's not easy for computers to translate between binary and decimal. It is, however, easy for them to translate between binary and hex.

**So what IS Hex, exactly?**

A hexadecimal number system uses a base of 16. At first, this may seem odd, but actually it's very straightforward. A group of 16 bits can be divided into 4 groups of 4 (a nybble), and of course 4 is the square of 2 (binary). And in fact, each hexadecimal digit stands for a group of 4 binary ones.

Four binary digits can be used to count up to 15. So if we are going to use one Hexadecimal digit to represent numbers of up to 15, we are going to need some more symbols.

In decimal counting, we run out of symbols after 0-9. But in Hex, we need to be able to go up to 15- so we add on the letters A, B, C, D, E and F after the 9.

This means that -

- A = 10

- B = 11

- C = 12

- D = 13

- E = 14

- F = 15

The decimal number 16 in Hexadecimal would be 10 - 1 lot of 16, and 0 lots of 1.

In fact, you can write out columns exactly as we did in binary - but this time, the columns will increment in powers of 16, like this -

Remember that you can now have up to 15 in each column before you have to move one over to the left, so your number might look like this -

This means that we have (1 x 65536) + (10 x 4096) + (5 x 256) + (3 x 16) + (15 x 1). The total is 107839.

**The Other Way..**.

Going from decimal to hex is slightly harder. Start off by writing out your columns, as before -

Now take your "target" number, for example 67951. We can get 1 "lot" of 65536 out of it, leaving (97951-65536) = 2415. Write 1 under the 65536 column.

We don't have enough left to make 4096, so put a zero in there. We will, however, get 9 lots of 256 out of it (2304), leaving 111 still to allocate between the columns.

6 lots of 16 make 96, leaving 15 to place in the 1's column.

Phew! That was tough!

**Let's try something easier...**

You will be pleased to hear that moving from binary to hex, and back again, is a **lot **easier.

We already saw that each hexadecimal digit can be replaced with four binary ones. Using the number we worked out above - 1096F - we can take each digit in turn, treating it as a separate number, and convert to binary, like so -

Then just string the binary groups together -

Moving from binary to hex is just as easy -

Treat each nybble as a separate digit, and convert it...

So your answer would be A5EB1*h*. (It is usual to put a lowercase *h* on the end, so that the unwary will know that you are displaying a hexadecimal number!)

Now how simple was that?! If you need more practice, I would suggest making up some numbers by yourself, and practice converting them between decimal, binary and hexadecimal bases. You can use the calculator in Windows to check your answers. Go to Programs>>Accessories>>Calculator, View, Scientific Format. It will look like this -

Put your decimal-base number in, then just click on the radio buttons to show the same number in Hex, Octal (base 8) or Binary.

Now that you can do complex transitions, it's time to learn how to add and subtract numbers in the different bases.