Difficulty: Beginner

Contents

  • What are number bases?
  • Why do we use different number bases in computer programming?

What are number bases?

A number base is a mathematical term which explains how to interpret a series of digits, as a numerical value.

Most of us will have been taught to use a base 10 numbering system, although you may not be aware of this. A base 10 numbering system means that each single digit will represent one of 10 possible values (0-9).

In school you probably learned that any number – let's take 2147 – can be broken down into columns to show that it is equal to 2x1000 + 1x100 + 4x10 + 7x1. These columns represent the orders of magnitude of our base number. The units column represents our base number of 10, with an exponent of 0. Our tens column represents our base number of 10 with an exponent of 1. This is shown in the table below. 

Column Thousands Hundreds Tens Units
Baseorder 103 102 101 100
Digit 2 1 4 7
Value

2x103

2000

1x102

100

4x101

40

7x100

7

Binary is simple an alternative number base to use which has only two possible values per digit – 0 or 1. We can break any number down into its component digits just like we can with base 10, this is demonstrated in the table below using the same value of 2147. If you sum up the representative values in the value row you'll find that it equates to exactly the same as when we represent the value in base 10. This shows us that base 10 "2147" is equivalent to base 2 "100001100011".

Baseorder 211 210 29 28 27 26 25 24 23 22 21 20
Digit 1 0 0 0 0 1 1 0 0 0 1 1
Value

1x211

2048

0x210

0

0x29

0

0x28

0

0x27

0

1x26

64

1x25

32

0x24

0

0x23

0

0x22

0

1x21

2

1x20

1

 

So why do we use base 2 (and other number bases) in computer programming?

First lets not think of all the complexity of a computer system, and instead focus on a single, simple circuit.

Circuit with one lamp and one switch
State count Lamp state
0 0 (off)
1 1 (on)


Above is a circuit diagram which shows a power source, a switch and a lamp. This circuit has different operating states, the lamp will be off when the switch is open, or the lamp will be on when the switch is closed. Next to the circuit below we have listed the states in a table. Notice we've started counting our states from zero instead of 1 as you might initially be tempted to do, and we have also labelled our off state as "0" and our on state as "1". The reasoning for why we've done this will be explained shortly.

Now of course this circuit isn't very interesting, so lets take a look at what would happen if we were to add more states to this circuit and see how we might list these states in a table.

Circuit with two parallel branches, each with a switch and a lamp
State count Lamp 1 state Lamp 0 state
0 0 (off) 0 (off)
1 0 (off) 1 (on)
2 1 (on) 0 (off)
3 1 (on) 1 (on)
Now that our circuit contains two lamps which can be independently controlled via their respective switches. This gives have a total of 4 different states which we've counted in a similar method as before, and we've labelled the lamps so we can see exactly how the table states correlate to the diagram.

If you have a keen eye you may notice that the table layout looks very similar to the base two number example that we've shown above. If instead of "lamp 0" we had 20 and multiply this by the state of that lamp (0 or 1), then repeat the process for "lamp 1" (which maps to 21 multiplied by the state in that row) the resulting number that we obtain by adding together all the states in the circuit is the same as our state count. This is why we represent off and on as 0 and 1 respectively, and it's also a major reason in computer science that we generally start counting from zero instead of one - our default off state for all digits sums to zero.

This idea can be extended to as many digits as required (as shown with the number example previously), with each digit representing a single wire state of off or on (0 or 1 respectively). If we want to represent multiple wires then we can simply add an additional column to our number. Below is a table showing how 4 wires would be represented in base 10 (decimal) and base 2 (binary).

Decimal Value Binary Value Wire 3 Wire 2 Wire 1 Wire 0
23 22 21 20
0 0000 0 0 0 0
1 0001 0 0 0 1
2 0010 0 0 1 0
3 0011 0 0 1 1
4 0100 0 1 0 0
5 0101 0 1 0 1
6 0110 0 1 1 0
7 0111 0 1 1 1
8 1000 1 0 0 0
9 1001 1 0 0 1
10 1010 1 0 1 0
11 1011 1 0 1 1
12 1100 1 1 0 0
13 1101 1 1 0 1
14 1110 1 1 1 0
15 1111 1 1 1 1