Number Bases for Computing

There is a simpler explanation of number bases, along with interactive pages on binary, binary fractions, normalised floating-point binary, and hexadecimal in the Interactive section.  There is also a number bases Abacus that you can use to experiment with different number bases and you can watch videos on number bases on the AdvancedICT YouTube channel. You are required to convert between number bases for most GCSE Computer Science courses.

What is a number base?

A German mathematician called Leopold Kronecker once said that "God gave us the integers, and all the rest is the work of man." I think what he was getting at is that numbers exist, but we can choose to represent them in different ways, and do different things with them. Without getting too philosophical, it helps to remember that, often, what we think of as numbers are actually symbols representing the number - just as the way you write your name isn't you, and you're still the same person if you type your name, write it in a different colour, or use a different alphabet.

Number bases are different ways of writing and using the same number. We use a system called base 10, or denary, for our arithmetic, but there are almost as many number bases as there are numbers. Many people think that we use base 10 because we have 10 fingers on which we can count. Computers, and other electronic devices, can only reliably use an electrical current, or the absence of a current, to count (like having two fingers), and so they tend to use base 2 (binary) internally.

The important thing to remember is that number bases are just different ways of writing down numbers - just as Roman Numerals or tally charts are - but other than that the numbers behave as normal. This doesn't mean that their arithmetic is fundamentally different - in fact, the way that the number bases behave is entirely consistent.

How do they work?

You will be familiar with the idea of base 10, and the column headings you used to talk about at primary school - units, tens, hundreds, thousands, etc. - other number bases work in exactly the same way. They have columns, with headings, that are used to represent numbers, and different digits are placed in different positions, starting at the right.

The column headings for the common bases, and the generic base x, are shown in the table below:

Column Headings Number Base
128 64321684212 (Binary)
etc. 2621443276840965126481 8 (Octal)
 etc.10000010000100010010110 (Denary)
  etc.65536409625616116 (Hexadecimal)
etc. X6X5X4X3X2X1X0 X

The base is usually written as a subscript after the number, so you can tell that 1112 is 7 in binary, and not one hundred and eleven.

You can work in any number base (except 1, which wouldn't really make sense), and some programming languages such as Lisp let you do that. In computing, however, you generally only come across the following four bases, and you know base 10 already. These common bases also have proper names, shown in parentheses:

The largest digit you can have in any column is the one less than the number of the base. So for binary (base 2) it's 1, then 7 for Octal (base 8), 9 for Denary (base 10), etc.

After base 10, however, we run out of digits to represent the numbers, so we have to use letters, where A = 10, B = 11, C = 12, etc. So the sequence for numbers written in Hexadecimal is 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, followed by 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, etc.

When we write a number in base 10, we know its value because we multiply the individual digits by their corresponding column headings. For example, when we see 123, even if we don't think about it, we calculate 1 x 100 + 2 x 10 + 3 x 1 to give us one hundred and twenty three. Other number bases work in exactly the same way.

You can use this information to convert numbers in other bases to base 10, e.g.:

32168421 
  10112= 8 + 2 + 1 = 1110

327685126481 
 12348= 1 x 512 + 2 x 64 + 3 x 8 + 4 x 110
= 512 + 128 + 24 + 410
66810

Binary (Base 2)

You probably won't encounter binary much these days, but it's useful to understand that this is how computers work internally, so you can understand concepts such as parallel transmission. The fact that computers use binary is why everything is a multiple of 2 - why computers come with 8Mb, 16Mb, 32Mb, 64Mb, etc., of memory, rather than 10Mb, 20Mb, 30Mb, etc., and also why there are 1024 bytes in a kilobyte (1024 = 210) rather than 1000 bytes.

It's main use is probably in combination with the bitwise logic techniques shown on the previous page, to combine and split values stored in the same byte (or word).

There are some other binary-related terms you'll need to know. Firstly, a bit is a binary digit - i.e. a single occurrence of 0 or 1. This is the smallest unit of storage you can have inside a computer. Groups of 8 bits are called bytes. A byte can be used to represent a number, or a colour, or a character (e.g. using ASCII). You may also hear the term nibble, which is 4 bits. Finally, a word is the largest numbers of bits that a processor can handle in one go - for example, when we say that new computers have 64-bit processors, we mean that the word length is 64-bits, or 8 bytes.

The largest value that you can store using a particular numbers of bits can be determined quite easily. Using n bits, the largest value you can store is 2n - 1, and the number of different values you can store is 2n (from 1 to 2n - 1, and then 0 as well). So using 8 bits, the largest number you can store is 28 - 1 = 255, and the number of possibly values is 28 = 256 (i.e. 0 - 255). A 32-bit computer can therefore handle values up to 4,194,967,296 in one clock cycle - it can obviously cope with larger numbers, but it would need to split them up first.

Octal (Base 8)

I'd never come across anything that uses octal, but a user of this site got in touch via the contact page and put me straight - thanks Robert!

One note though concerning Octal and its usage - PLCs often use Octal in addressing their I/O, particularly with the AB PLC-5, which, although retiring, is still in wide use today.

I think it's probably also included on exam specifications for academic reasons, e.g. because it's easy to convert into binary (see below).

Hexadecimal (Base 16)

Hexadecimal is still used quite a lot - particularly for things like colours in HTML or programming languages. It's also quite useful because representations of large numbers are relatively compact, but are easily converted to binary so that you can see the bit patterns.

Shifting Bits

You've no doubt noticed that with numbers in base 10, you can move the digits left or right one place by multiplying or dividing the number by 10. The same trick works with different number bases - you just multiply and divide by the base number (e.g. multiply by 2 in binary to shift the bits left one place).

This can be useful for things like creating hexadecimal colour values (e.g. for web pages). In a 24-bit system (such as HTML), colours are represented by 24-bit numbers from 000000 to FFFFFF (each hexadecimal digit corresponds to 4 bits - see below). The 24 bits are made up of 8 bits each for the amount of red, green and blue in the colour.

So, each component is represented by 8 bits - i.e. a number from 0 to 255. If you know how much red, green and blue you want, how do you combine them to find the complete colour? For HTML, the correct order of the bits is RRGGBB (r = red, g = green, b = blue), so what we need to do is "shift" the values of green and red components, and then add all three components together.

We can leave the blue value as it is, but we need to move the green value along two places. To move along one place in hexadecimal, we multiply by 16, so to move along two places, just do it twice - 16 x 16 = 256 - so multiply the green value by 256. For the red value, we need to move four places - 16 x 16 x 16 x 16 = 65,536 - so we multiply the value of the red component by 65,536.

Obviously, if you were just trying to work out the colour yourself, you wouldn't need to go through these steps, but if you were to create a program like my colour mixer, then this is how you'd do it.

Converting Between Bases

You can convert from Octal to Binary and vice-versa by grouping this binary digits (or bits) into threes, and then changing them into their binary equivalents. For example:

What is 1011002 in Octal?

1012 = 510

1002 = 410
Just combine the two digits:

1011002 = 548

The same thing can be done for Hexadecimal, only the bits must be grouped into fours, e.g.

What is 101101102 in Hexadecimal?

10112 = 1110 = B16

01102 = 610
Just combine the two results:

101101102 = B616

Finally, a number base joke! Why do programmers confuse Christmas with Halloween? Because 31 OCT = 25 DEC!