Useful pages - index

# Number bases explained

There isn't really anything stunningly original on this page. This is basically an overview of what I learned when I was about 11. However, I've been quite shocked by the number of people who haven't been taught about how bases work, so this is my attempt to balance the scales a bit.

N.B. Before you can understand bases, you need to understand powers.

## Overview

Okay, first things first. The system we use normally is called base 10, or denary. I'll start with an overview of this, and then move on to other systems later. This should be familiar to you, but it's worth reviewing. Suppose you have a number like:

729

To understand this, we go from right to left. The first column is the units column. The second column is the 10s column. The third column is the 100s column. So then, this figure is (7 times 100) + (2 times 10) + (9 units). To put it another way, this is (7 * 10^2) + (2 * 10^1) + (9 * 10^0).

Although this number only has three columns, you can actually have as many as you want, and just keep extending the same pattern. As you saw above, the first column shows the quantity of 10^0, the second column shows the quantity of 10^1, and the third column shows the quantity of 10^2. Generalising this, column number X shows the quantity of 10^(X-1). For example, the fifth column shows the quantity of 10^(5-1), i.e. 10^4, i.e. 10,000. Similarly, the fifteenth column shows the quantity of 10^14, i.e. 100,000,000,000,000.

As I said, this system is base 10, and so each column shows the quantity of a power of 10. There are other bases around too, the most common being binary (base 2), octal (base 8), and hexadecimal (also known as hex) (base 16). These systems work on the same basis - if you are in base Y, each column shows the quantity of a power of Y.

So, as a general rule, column number X in base Y shows the quantity of Y^(X-1). Don't worry if this sounds too technical - I'll try to clarify it by some more examples.

To give (another!) general rule, a particular base will include all the numbers below the base's number. So, base 10 only includes the digits 0-9 in each column. Actually, I think that base 10 is something of a misnomer, since (as we shall see later), 10 is the only number to appear in every base, so I would prefer to see denary referred to as base A. But that's just me being weird again. Anyway, I'm probably confusing you again, so it's time for another example. Base 2 (binary) only contains the digits 0 and 1. Base 5 only contains the digits 0-4. Base 8 (octal) only contains the digits 0-7. And so on.

Counting in other bases is much the same as counting in denary. You start in the first (i.e. rightmost) column, go through all the available digits, then when you run out you stick a 1 in the column to the left, and change the current column to zero. You then repeat this as often as necessary. So, in binary, you start with 0, then 1, as normal. However, since 2 doesn't exist in binary, you then go to 10, and 11. Again, you can't have twelve, so you then jump to 100. An alternate way to think about this is to list all the "numbers", and skip the ones which contain anything other than 0 and 1.

Similarly, if we count in octal, we go 0, 1, 2, 3, 4, 5, 6, 7, 10, 11, and so on. Hexadecimal is a bit more complicated, as when you get past 9 you need to keep the numbers in one column. So, for ten onwards, we use letters of the alphabet: A is ten, B is eleven, C is twelve, D is thirteen, E is fourteen, and F is fifteen. We don't use G for sixteen, as that is the number of the base, so instead we use 10. As I said above, this number appears in every base, so it can get confusing. Generally, you would see it written with a small 16 to the right of it, to show that this is a hex value. There are other notations, such as putting an H in front (similarly an O (this is oh, not a zero) for octal). Generally, however, you will know which base you're dealing with from the context.

To return to my theory for a moment, I would like to see "base 10" referred to as "base A".

## Converting from another base to denary

Anyway, getting back to more practical matters, how do you translate a value from a different base? For example, suppose we have the binary value 10110, and we want to represent this in denary. To do this, we use the formula I gave above.

Starting from the right, column 1 represents the quantity of 2^0. Similarly, column 2 represents the quantity of 2^1, column 3 represents the quantity of 2^2, and so on. Applying this to our number, we have:

Column 1: 0 * 2^0 = 0 * 1 = 0
Column 2: 1 * 2^1 = 1 * 2 = 2
Column 3: 1 * 2^2 = 1 * 4 = 4
Column 4: 0 * 2^3 = 0 * 8 = 0
Column 5: 1 * 2^4 = 1 * 16 = 16

As another aside, if you were actually working this out in practice, you wouldn't need to work out the powers for the columns that contain zero. I have only done this to show you the principles in action.

So, adding up the denary value of each column, we have 0 + 2 + 4 + 0 + 16 = 22.

Using the same principle for other bases:
245 in octal = (2 * 8^2) + (4 * 8^1) + (5 * 8^0) = (2 * 64) + (4 * 8) + (5 * 1) = 128 + 32 + 5 = 165 in denary.
7B3 in hex = (7 * 16^2) + (11 * 16^1) + (3 * 16^0) = (7 * 256) + (11 * 16) + (3 * 1) = 1792 + 176 + 3 = 1971 in denary.

## Converting denary to another base

So, what happens when we want to go in the opposite direction? To convert from a denary number to a different base, you basically have to work out the quantity of each power of that other base. You could theoretically convert directly from (say) base 8 to base 5, but it is probably easiest to convert the octal number into denary, then convert the denary number into base 5, since denary will be most natural for you.

For example, suppose you want to convert 97 from denary to binary. You then need to work out the quantity of each relevant power of 2. I say relevant, because lots of the high powers aren't going to be needed. For example, 2^9 is 512, which is bigger than 97, so the quantity of 2^9 is going to be zero. Obviously, this will also mean that for any power above 9, the quantity will also be zero, since the bigger the power the bigger the number. So, the idea is to start out at zero, and work up through the powers until you hit one that gives a bigger number than the one you're converting.

In this case:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128

2^7 (128) is bigger than 97 (the number we're converting), so we can stop here. We will want to see the quantities of each power between 0 and 6, which will give us a 7 digit binary number (one column per power). To work out the quantities, we move downward through the list. In each case, if the number we're working with is greater than or equal to the value of 2 to this power, we want a 1 here (as there is one of them). If we were working with a different power, it wouldn't necessarily be 1 - it could be a higher number, as I'll demonstrate shortly.

2^6: 97 > 64, so we want a 1 in this column. We then subtract 64 from 97, leaving 33, which we pass on.
2^5: 33 > 32, so we want a 1 in this column too. We then subtract 32 from 33, leaving 1, which we pass on.
2^4: 1 < 16, so we want a 0 in this column. We don't subtract anything when there is a zero quantity.
2^3: 1 < 8, so again we want 0 in this column, and we don't subtract anything.
2^2: 1 < 4, so again we want 0 in this column, and we don't subtract anything.
2^1: 1 < 2, so again we want 0 in this column, and we don't subtract anything.
2^0: 1 = 1, so we want a 1 in this column. We subtract 1 from 1, leaving zero, so our conversion is complete.

Therefore, 97 in denary corresponds to 1100001 in binary.

To take a more complex example in a higher base, let's try to convert the same number (97) into octal. Again, we start by working out the powers of 8.

8^0 = 1
8^1 = 8
8^2 = 64
8^3 = 512

8^3 (512) is bigger than 97 (the number we're converting), so we can stop here. We will want to see the quantities of each power between 0 and 2, which will give us a 3 digit octal number (one column per power). To work out the quantities, we move downward through the list. In each case, if the number we're working with is greater than or equal to the value of 8 to this power, we want to show how many of this power there are.

8^2: 97 > 64, so find out how many 64s are less than 97. This is 1 (since 2 * 64 = 128, and 128 > 97). So, we put 1 in this column, then subtract 64 from 97, leaving 33, which we pass on.
8^1: 33 > 8, so find out how many 8s are less than 33. This is 4, so we put 4 in this column, then subtract 32 (4 * 8) from 33, leaving 1, which we pass on.
8^0: 1 = 1, so we want a 1 in this column. We subtract 1 from 1, leaving zero, so our conversion is complete.

Therefore, 97 in denary corresponds to 141 in octal.

## The relationship between hex and binary

The previous section explains how to convert numbers between any two bases, so if you wanted to convert a number from binary to hex then that approach would work. However, in this particular case there is a shortcut that you can use. The significant point here is that 16 is 2 to the power of 4. That means that every digit in hex is equivalent to four digits in binary. This makes it quite convenient to represent numbers when you're dealing with computers - you have two hex digits for every byte.

To give you an example, here is an eight digit binary number (one byte):

01011101

Going from right to left, this means:

1 times 2^0 (1)
0 times 2^1 (2)
1 times 2^2 (4)
1 times 2^3 (8)
1 times 2^4 (16)
0 times 2^5 (32)
1 times 2^6 (64)
0 times 2^7 (128)

In denary, this equals 93. The general purpose way to convert this into hex is to go through the powers of 16 until you find one that's greater than or equal to this number. So:

16^0 = 1 (<93)
16^1 = 16 (<93)
16^2 = 256 (>93)

This means that the hex number will only involve two digits, as the smallest three digit number is 100, i.e. 256 in denary, which is too high. So, you then work from left to right:

Divide 93 by 16: this gives 5.8125
Chuck away everything after the decimal point, and you get 5 - this is your first digit.

Now calculate 16 * 5 (80), and subtract this from 93, to get 13. In other words, 16 divides into 93 five times, with a remainder of 13. This is your next digit, except that it's in hex, so it's D.

In other words, 01011101 in binary = 93 in denary = 5D in hex.

As I mentioned above, however, there is a shortcut in this case, since 16 is a power of 2. (A similar principle applies to other powers of 2, e.g. octal (base 8).) All you have to do is take each group of four digits, and replace it with the corresponding hex digit.

So:

0101 (binary) = 1 + 4 (denary) = 5 (denary) = 5 (hex)
1101 (binary) = 1 + 4 + 8 (denary) = 13 (denary) = D (hex)

So 0101 1101 (binary) = 5D (hex), which is the same result as above.

If the number of binary digits isn't a multiple of four, then add extra zeros at the start as necessary. For instance, take this binary number:

11000110111010

Split it up into groups of four digits, going from right to left:

11 0001 1011 1010

Now add two zeros to the left-most group:

0011 0001 1011 1010

Now you can convert it into hex as before:

3 1 B A

I haven't gone through the intermediate denary step explicitly here, but you should be able to figure it out. I've been doing this for a while, so I can just glance at a binary "quad" (i.e. group of four digits), and say "ah yes, 1011 = 1+2+8", without having to think "what's 2 to the power of 4?" With more practice, you'll be able to convert straight to hex at a glance, as there are only sixteen possible combinations.

If you are going in the opposite direction, i.e. hex to binary, then it's even easier. Simply replace each hex digit with the corresponding quad. E.g. take the hex number 13EA. Then break it up into the separate digits:

1 3 E A

Now go through each digit in turn, and replace it with the appropriate binary sequence. For each number, you know that it will be some combination of 1, 2, 4, and 8. So:

1 (hex) = 1 (denary) = 0001 (binary)
3 (hex) = 3 (denary) = 2 + 1 (denary) = 0011 (binary)
E (hex) = 14 (denary) = 8 + 4 + 2 (denary) = 1110 (binary)
A (hex) = 10 (denary) = 8 + 2 (denary) = 1010 (binary)

Thefore the binary equivalent is:

0001 0011 1110 1010

Well, here endeth the lesson. I hope that this has been useful to someone, and if you have any comments or questions please email me with them. One thing I hope to add to this page in the future is a program to convert numbers between different bases on the fly, to demonstrate the principles I've described.