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).
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) 10And 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.)
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).
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) FFHow do you write these numbers in hex?
(j) thirty (k) thirty-three (l) forty-sevenAnswers
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:
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) sixteenIs the following correct?
(p) 101 + 1 = 110Answers
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 ONA 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:
11000101This 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:
What is the hex representation of the following bytes?
(q) 10000001 (r) 00010000 (s) 10101010 (t) 11111111And 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) AEAnswers
Answers to questions on octal, hex and binary
(a) eleven (b) twenty nine (c) eight (d) 14 (e) 77 (f) 10 Return to text