Binary Conversions!

The theory behind numbers and how binary numbers work.

Introduction

In this section of the Binary Tutorial we will look at how to easily convert between binary, decimal, hexadecimal and octal numbers.

It's important to be able to translate a value from one number system into another. We need to be able to tell when two values represent the same amount. Sometimes, shortcuts become available to us. For instance octal and hexadecimal are often used as shorthand for binary (we'll see why below). Certain systems will only accept values in a certain base as well. Fortunately, switching between number systems is not too difficult.

There are many calculators now which can do conversions for you. Just about every desktop OS, smart phone and tablet has one built in or one can easily be aquired. It is perfectly fine to use the calculator but we should know how to do it by hand as well. This will give us a much better understanding as to what is actually happening. That understanding is important in order to understand how certain mechanisms work (especially in computing). My recommnedation is to practice on paper by hand but use the calculator to verify your working.

Binary to Decimal

We touched on binary to decimal conversion briefly in the previous section. We'll cover it again here. The process is fairly simple.

  • Each digit may only be a 1 or 0.
  • If it is a 0 then it adds nothing.
  • If it is a 1 then we add 2 to the power of that position.

The right most digit in the binary number is multiplied by 2 to the power of 0 ( which is 1 ). The next digit is multiplied by 2 to the power of 1 ( which is 2 ) and so on.

I find it easiest to start at the far right binary digit as this is always going to be to the power of 0. If you start at the far left digit then you have to work out what the starting power should be (how many digits there are minus 1) and it's easy to make a mistake. Starting at the far right also makes our calculations easier as each time we increase the power we simply double the result of the previous power.

  • 20 = 1
  • 21 = 2
  • 22 = 4
  • 23 = 8
  • 24 = 16
  • and, well, you get the idea.

Here is a simple demonstration that should make the process clearer. You may put your own number in or hit the button to generate a random number.

Running Total in Decimal :

Decimal to Binary

Converting a decimal number to binary is a little more complex. Once you get the hang of it though it's fairly straight forward. There are a few ways you may approach converting decimal to binary but the method I find most convenient is repeated division by 2.

With repeated division by 2 we repeatedly divide the number by 2 and record the remainder. Once you've done this, all the way down to 0, you read the remainder digits from bottom to top and you have your binary number. The example below will illustrate. The result of division by 2 is listed on the left and the remainder is listed on the right.

Result in Binary :

A common mistake is to read the remainders in the wrong direction. Remember: divide down then read up.

Another common mistake is to divide down to 1 then fail to make the final division. Remember to always divide all the way down to 0.

Remember that the only way a binary number may be odd is if the right most digit is 1. This characteristic may be used as a quick check to see if you've made a silly mistake (for converting either way).

The above tip doesn't guarantee that the answer is correct but if it doesn't fit you definitely know you have made a mistake.

Hexadecimal to Binary

Converting from binary to hexadecimal and hexadecimal to binary is actually very easy. This is because hexadecimal is base 16 which is actually 24.

As such, to convert a hexadecimal number to binary all we need to do is replace each hexadecimal digit with it's equivalent in binary. In the following examples it may help to refer to the reference table further down the page.

For example if we have the hexadecimal value 75C then it may be converted to binary as follows:
(Refer to the reference table further down if necessary.)

7
0111
5
0101
C
1100

Result: 11101011100

Each hexadecimal digit (except for the far left) must be replaced with 4 binary digits. If the digit (such as 5 above) normally only needs 3 binary digits we must pad it out with 0's accordingly. It's a common mistake to forget this.

Binary to Hexadecimal

To convert binary to hexadecimal is simply a matter of reversing the above process. Divide your binary number into chunks of 4 (starting from the far right), then replace each chunk with the relevant hexadecimal value.

Octal to Binary

Converting octal numbers to binary is similar to the process for hexadecimal. Octal is base 8 which is 23 so instead of breaking the binary number into chunks of 4, we break into chunks of 3.

In this example we'll conver the octal number 3625 into binary:

3
011
6
110
2
010
5
101

Result: 11110010101

Binary to Octal

Similar to hexadecimal, converting from binary to octal is just reversing the process. Again, remember to divide into chunks of 3 starting from the far right.

Hexadecimal and Octal to Decimal

If you need to convert between decimal and hexadecimal or octal it is possible using the methods listed for binary but replacing 2 with either 16 or 8. It will work but if you're like me you're not too good when it comes to your 16 times tables. A better approach is to use binary as a go between. It is easy to convert hexadecimal or octal to binary and then not too hard to go from binary to decimal.

It's a little bit more working but 2 easy steps is generally much better than 1 hard step.

Reference Table

Here is a reference table for the different number systems. It is useful for converting between binary and hexadecimal or octal (and vice versa).

Decimal Binary Octal Hexadecimal
0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F

You'll notice that there is a pattern to the binary. The far right column alternates between 0 and one. The next column does the same but 2 at a time. The third column does the same but 4 at a time. The far left column does the same but 8 at a time. This pattern makes it easy to verify it's been written correctly.

If you are doing an exam on binary you often aren't allowed to take material in but nothing is stopping you drawing this table out yourself after the exam has started. It can be a good reference , especially for conversions which we'll look at in the next section.

Advice

This stuff can be a little hard to get your head around. If you find reading through the material doing your head in a bit here's what I suggest:

  • Work through several activities below on paper. Learning binary is kinda like riding a bike. The best way is just to do it.
  • Leave it a day or two then come back and have another go.

Activities

So the best way to learn this stuff is to practice it and now we'll get you to do just that. For each of the activities below you can keep generating new numbers to try so keep working through them until your comfortable with them.

  • Binary to Decimal:
  • Decimal to Binary:
  • Hexadecimal to Binary:
  • Binary to Hexadecimal:
  • Octal to Binary:
  • Binary to Octal:
  • Hexadecimal to Decimal:
  • Decimal to Hexadecimal: