Back

Converting between positive denary whole numbers and binary

Introduction
The common number system that we use in our daily lives is called the 'denary' system, or counting using 'base 10'. In practice, what this means is that we count using 10 different digits: 0,1,2,3,4,5,6,7,8 and 9. It isn't just the digit that is important, however. It is the position of the digit relative to the other numbers.

For example, consider the digit 6. It is worth one amount if it is on its own. It is worth a different amount in 63, and worth a lot more in 6324, and a lot less in 54.68. In all these examples, the amount that the digit is worth is determined by where exactly it is in relation to the other digits in the number.

Let's look at a number, for example 658. How do we know how much this number is 'worth'? We know because without thinking about it, we judge the digits that make up the number relative to the other digits! When you were at primary school, you may have been helped to learn your numbers by writing down column headings for a particular number, just so you could get used to the idea that position is important. For example, consider what the number 658 means.

countdenary

Of course, when you are working doing maths you (probably) don't write down the headings anymore. You don't need to because you're so used to counting in base 10. Until you are experts in the other counting systems you need to look at, you should get into the habit of writing down the 'worth' of each position.

The binary number system
Computers make heavy use of the binary counting system. The reason for this is that the electronic circuits that make up a computer are essentially made up of millions and millions of switches. The switches can have two states - on or off (which we represent using 1 and 0). The actual position of any particular switch relative to other switches is also very important.

The base 10 system (denary) uses 10 digits, zero to nine. The base 2 system (binary) uses just 2 digits (or 'bits') zero and one. Working with just 1 bit would be a problem as we could only count up to 1. If we use groups of 8 bits together, we can start forming codes and these will be much easier to work with. A group of 8 bits has a special name. It is known as a byte. We will put the 8 bits together, and allocate a 'worth' to each bit, depending on its position relative to the other bits.

Here is one possible allocation of the 'worth' of each bit in my byte.

weighting

Remember, it is not just whether a bit is a one or a zero that is important. The position of the bit in the whole byte is also important. The bit on the left is 'worth' far more than the bit on the right, for example, just like the 6 on the left in the number 6596 (six thousand, five hundred and ninety six) is worth more than the 6 on the right side of the number.

Incidentally, you can see when I used the denary system, there was a pattern to working out the 'worth' of each position. It went 103, 102, 101 100 and so on. (Any number to the power of zero is one)! There is a similar pattern when using base 2, which is how I calculated the 'worth' of each bit position.

worth

Converting from denary to binary
Imagine I want to keep a record of the score of one set of throws in a darts match! The minimum score we need to hold is zero. The maximum scroe you can make using 3 darts is 180, and the score will always be an integer, a whole number i.e. you cannot score 34.5, for example. So now, to represent 180, I would set the bits as shown below. (Note: when I talk about 'setting a bit' I mean that it is made a 'one'. When I talk about resetting a bit, it is made a 'zero')

180

There are a number of ways you can work this out. The way that was used here is as follows:

    • the biggest number that I could fit into my target number of 180 was 128. So make that bit a 1.
    • that leaves me with 180 - 128 = 52.
    • 64 won't go into 52, so make that bit a 0.
    • 32 will go into 52, so make that bit a 1.
    • that leaves me with 52 - 32 = 20.
    • 16 will go into 20, so make that bit a 1.
    • that leaves me with 20 - 16 = 4.
    • 8 won't go into 4, so make that bit a 0.
    • 4 goes into 4, so make that bit a 1.
    • that leaves me with 4 - 4 = 0.
    • so make the remaining bits 0.

Always start by trying to fit the biggest bit position into your number. Work from left to right, not right to left.

Converting from binary to denary
If you want to convert from binary to denary, you should

    • First of all write down how much each bit position is worth.
    • Then write the binary number underneath.
    • Finally, do the calculations.

For example, to convert 01001011, write down the weightings for each position, then write down the bits underneath the weightings, like this:

75

Finally, add up the bits which have a one under them: 64 + 8 + 2 +1 = 7510

Maximum and minimum values
The maximum number that I can represent using my system is 255. I don't need numbers this high for a darts score using 3 darts (the biggest number I need to hold is 180), but it is always nice to know what the maximum number you can store is for any given number of bits. You can see how to do this below.

255

It's also nice to know what the smallest number is using any number system. The smallest number you can represent here is zero. How you do this is shown below.

0

So using one byte, I can represent any number between 0 and 255 inclusive. If I had a nibble, what is the biggest and smallest number I can represent? The smallest is easy. It's when all the bits are zero so the total must add up to zero. The largest number is when all four bits in the nibble are 1, so the answer is 15.

For the mathematician amongst you, however, the biggest number that I can represent with 8 bits (a byte) is 28-1. Using four bits (a nibble), it is 24-1. For n bits, it is 2n-1.

Back