# MSc/PGDip Computer Science: A short lesson on octal, hex and binary

The first thing to grasp is that there are many ways of writing the same number. When you see "15", you think "fifteen", but it is only by convention that we take the characters "1" and "5", arranged side by side in that order (not "51"), to represent the number fifteen. In Roman numerals, the characters "XV" are used to represent fifteen. Other systems use other characters. This lesson is about three of these other systems.

The octal, hexadecimal ("hex" for short) and binary systems can sometimes look like the decimal system we are accustomed to, and this can cause some confusion, so I will first remind you how the decimal system works.

Suppose a farmer is counting his sheep as they return to the fold. For each sheep that passes, he holds one finger (or thumb) in the air. When all his fingers-and-thumbs are in the air, he scratches a cross on the wall and begins again with his fingers. If he ends up with, say, two crosses and three fingers, then twenty-three sheep have passed. Using the system of ten single numerals "0" to "9", we represent this as "23", the "2" standing for "number of crosses" (ie number of tens) and the "3" for "number of fingers" (ie units).

### Octal

Imagine now that human beings had only a thumb and three fingers on each hand. And suppose that the farmer ended up, as before, holding up three fingers and with two crosses scratched on the wall. How many sheep would this represent?

A cross represents all the fingers and thumbs on two hands, ie eight. So two crosses represent sixteen, which, with the extra three fingers, makes a total of nineteen.

Now suppose the farmer was a bit more sophisticated and used a system of numerals "0", "1", "2" etc rather than crosses and fingers. He would represent this number as "23", i.e. two crosses (two eights) plus three. He would look at "23" and think "nineteen". (What I mean is that he would think of the number that we call "nineteen"; he would probably have a different name for it, something like "twocty-three".)

It is because human beings happen to have two hands with four fingers, plus one thumb, on each hand - i.e. ten things to stick up in the air to count on - that the number ten occupies such a significant place in our system of naming and writing numbers. If we had had eight fingers-and-thumbs, rather than ten, the number eight would have occupied that special place. The system based on eight rather than ten is called the octal system.

Given that the following are written in octal, what numbers do they represent?

```(a) 13
(b) 35
(c) 10
```
And how do you write the following in octal?
```(d) twelve
(e) sixty-three
(f) eight
```
(Try to answer the questions before you look at the answers, which are at the end of these notes.)

#### 10 and 100

What does "8" represent in octal? Trick question. "8" is not used in octal, nor is "9". When the eight-fingers-and-thumbs farmer gets to eight, he scratches a cross on the wall and starts again, i.e. eight is represented by one cross and no fingers, or "10". In octal, "10" is how you write the number eight. It looks like ten to us, but that is because we are so accustomed to the decimal system.

On the same principle, when we reach "77" in octal (seven eights plus seven, ie sixty-three), the next number is "100" (one lot of eight-times-eight). In the decimal system, "100" is one hundred (ten times ten), but in the octal system, "100" is sixty-four (eight times eight).

### Hexadecimal

In the familiar decimal system, the number ten has a special place; in the octal system, the number eight has this place. In the hexadecimal system, it is the number sixteen that occupies this place.

We begin writing the numbers, as in the decimal system, from "0", "1" and so on up to "9". But we do not then go on to "10", i.e. we do not write the number ten as "10". Instead we start using capital letters, "A" for ten, "B" for eleven and so on until we have used "F" for fifteen. At this point we scratch a cross on the wall and start again, i.e. we write sixteen as "10", seventeen as "11" and so on.

If we were using hex and we wrote "23", this would mean two sixteens and three units, i.e. thirty-five. "2A", which does not at first sight look like a numeral at all, would mean two sixteens and ten units, i.e. forty-two. "AB" would mean ten sixteens and eleven units, i.e. one hundred and seventy-one. Assuming that the following are in hex, what numbers do they represent?

```(g) D
(h) 13
(i) FF
```
How do you write these numbers in hex?
```(j) thirty
(k) thirty-three
(l) forty-seven
```
Answers

### Binary

We have looked at a base-ten system (decimal), base-eight (octal) and base-sixteen (hex). The binary system is base-two. It makes do with just two characters - "0" and "1". In a base-ten system, ten is written "10"; in a base-eight system, eight is written "10"; in a base-sixteen system, sixteen is written "10". In the binary system, two is written "10". The farmer's cross on the wall represents just two in binary, so "10" (one cross and no units) stands for one two and no units, i.e. two.

Three in binary is "11", and already we have encountered the problem we reach at "99" (decimal), "77" (octal) and "FF" (hex); we need another digit, i.e. a three-figure numeral instead of a two-figure numeral. So, to represent four in binary we write "100", i.e. one lot of two-times-two. The following table goes up to eight in binary:

 nought 0 one 1 two 10 three 11 four 100 five 101 six 110 seven 111 eight 1000
Note that, while "1000" in decimal means "one lot of ten-times-ten-times-ten", "1000" in binary means "one lot of two-times-two-times-two", i.e. eight.

When we read a number in decimal - say "9036" - we take the digits from left to right and we know, from their position, that the "9" indicates the number of thousands, the "0" the number of hundreds, the "3" the number of tens and the "6" the number of units. When we read a number in binary - say "1101" - we know, from their position, that the leftmost "1" indicates the number of eights, the next the number of fours, the "0" the number of twos and the final "1" the number of units - eight plus four plus one, i.e. thirteen.

How do you write the following numbers in binary?

```(m) nine
(n) fifteen
(o) sixteen
```
Is the following correct?
```(p) 101 + 1 = 110
```
Answers

### Hex and binary

We have been considering how numbers can be represented in hex and binary. But hex and binary also have another slightly different use in computing, namely to represent the contents of a computer's memory.

You can think of the memory (or "main store") of a computer as consisting of an enormous number of little rows of switches. Each little row contains just eight switches and is called a "byte" of storage. Each switch, like an ordinary light switch, is either ON or OFF. To describe the state of a single byte in memory at a particular time, you could state the setting of each of the eight switches, perhaps like this:

```ON ON OFF OFF OFF ON OFF ON
```
A more concise method would be to use "1" for "ON" and "0" for "OFF". Using this system, the same set of switches would be described as:
```11000101
```
This may be an improvement on writing lots of "ON"s and "OFF"s but it is still far from ideal; strings of binary digits (or "bits") are tiresome to write and hard to read. Consequently it is common practice to take the eight binary digits in two groups of four and to represent each group of four by a single hex digit. The above example would be grouped into 1100 and 0101. 1100 is binary twelve and the hex representation of twelve is C; 0101 is binary for five, and hex five is 5. So 11000101 can be represented simply as C5.

Note that we are not saying anything about the significance of this byte to the computer. It may represent a letter in the middle of some text, or it may be storing part of a number or it may be part of one of the computer's instructions or it may be something else again. We are simply using the hex system as a shorthand to describe the setting of the eight switches.

The following table shows the binary and hex representations of the numbers nought to fifteen:

 nought 0000 0 one 0001 1 two 0010 2 three 0011 3 four 0100 4 five 0101 5 six 0110 6 seven 0111 7 eight 1000 8 nine 1001 9 ten 1010 A eleven 1011 B twelve 1100 C thirteen 1101 D fourteen 1110 E fifteen 1111 F

What is the hex representation of the following bytes?

```(q) 10000001
(r) 00010000
(s) 10101010
(t) 11111111
```
And what are the contents of the bytes represented by these pairs of hex digits?
```(u) 55
(v) 7F
(w) 00
(x) 18
(y) BC
(z) AE
```
Answers
```

```

### Answers to questions on octal, hex and binary

```(a)	eleven
(b)	twenty nine
(c)	eight
(d)	14
(e)	77
(f)	10

Return to text

Hexadecimal

(g)	thirteen
(h)	nineteen
(i)	two hundred and fifty five
(j)	1E
(k)	21
(l)	2F

Return to text

Binary
(m)	1001
(n)	1111
(o)	10000
(p)	Yes if the numbers are in binary (five plus one equals six).

Return to text

Hex representation of bytes
(q)	81
(r)	10
(s)	AA
(t)	FF
(u)	01010101
(v)	01111111
(w)	00000000
(x)	00011000
(y)	10111100
(z)	10101110
```