# How to convert binary to decimal (and vice versa)

Humans have spent the last five and a half millennia inventing over 100 different ways to write numbers.

With all due respect to Roman numerals, the world’s favorite technique right now is — by far — the modern decimal system. Its users can express any whole number they like with just 10 small characters: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9.

But your computer takes another approach.

Laptops, smartphones and other devices depend binary code. A mathematical language, binary transmits instructions to these high-tech gadgets. It tells your computer how a podcaster’s voice sounds, what colors should appear in a YouTube video, and the number of letters used in the email your boss just sent.

You know. Important stuff like that.

Binary code is aptly named. Unlike the decimal system, it only uses two types of digits, which programmers call “parts.” Usually there is “0” and there is “1.” And that’s it.

Today we’ll show you how to convert a binary number into the more familiar decimal system. Then, like a good magicianwe will do the exact opposite.

Do not worry. With a little patience, you can master this “trick” – and have fun doing it!

### Know your exhibitors

Technically, 0 and 1 are the only bits you need to write binary numbers. But to *give a meaning* among them you must understand a third value: 2.

It is better that we explain this by way of example. The number **138** is correctly expressed in binary code as “**10001010**.”

How can your computer tell that this apparent string of gibberish means “138”?

Programming is part of the answer. Someone has informed your device that – in this case – the binary code speaks a *Number* instead of a written word or sentence; There are a separate method to decode it.

Once this basic fact is established, the code works by assigning an exponent different from 2 to each individual bit (i.e. all 0s and all 1s).

An exponent is a value multiplied by itself a certain number of times. So 2 to the third power, written as **2**^{3}is 2 x 2 x 2, which equals 8.

Please enjoy the following powers of 2 listing. Trust us, you’ll want to check it out soon.

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^{8} = 256

2^{9} = 512

2^{ten} = 1024

### Binary to decimal conversion

Now back to our original binary number: **10001010**

If English is your first language, get ready. You are about to fight against your instincts. You see, written English is read from left to right. But now we need to decompose this binary number by going in the opposite direction: from right to left.

In any binary number, the rightmost bit must be multiplied by 2^{0}. Then the one to its immediate left is multiplied by 2^{1}.

Then the little to *his left* is multiplied by 2^{2}. And so on.

Notice a pattern here? Individual exponents of 2 are used in ascending order, *from right to left*.

OK, so now our job is to maintain this pattern until we’ve matched an exponent of 2 to each bit – each 0 and 1 – in the binary number.

We’ll stop once the last bit, the leftmost one, has been multiplied by the appropriate exponent of 2.

A useful way to keep your numbers straight is to physically align the exponents to their corresponding binary bits on a piece of paper. Ideally, it should look like this:

Good product. Alright, now back to the actual conversion.

Because 10001010 contains 8 individual bits, we are going to do 8 separate multiplication problems. Let’s start with the 0 on the far right.

What is 0x2^{0}? The correct answer is 0.

One less problem, seven to go. Move one square to the left. Do you see the “1”? Well, 1 x 2^{1} = 2. Now move yet another space to the left. This will give you 0 x 2^{2}which is 0.

If you continue to use this template, working from far right to far left, here’s what you’ll see:

0x2^{0} = 0

1×2^{1} = 2

0x2^{2} = 0

1×2^{3} = 8

0x2^{4} = 0

0x2^{5} = 0

0x2^{6} = 0

1×2^{7} = 128

Wait, we’re almost at the finish line!

Take the results of all these multiplication problems and add them together. Do not multiply, *add*. Clever?

What is 0 + 2 + 0 + 8 + 0 + 0 + 0 + 128?

Before we answer that, let’s get rid of all those zeros. We don’t need it in an addition problem. All we really have to do is solve this puzzle:

**2 + 8 + 128 = ?**

Guess what? The final answer is **138**. Congratulations, we have come full circle! Go for a lap of honor.

Note that 138 is an integer. There is a technique to convert numbers with a fractional component, like 0.25 and 3.14, to binary. But full disclosure: it’s a bit complicated. If you don’t mind and want to learn more, the Institute of Electrical and Electronics Engineers (IEEE) has published a standardized conversion method.

Here is a handy widget to help you with conversions.

### Conversion from decimal to binary

After changing “10001010” to “138”, it’s time to reverse our process. Suppose you started with 138 and needed to convert it to binary. How would you do?

Again, exhibitors are the key to the whole shebang.

Take another look at our “powers of 2” list. Find the value that is closest to 138 *without exceeding it*.

A quick re-reading tells us that 138 are between 256 (i.e. 2^{8}) and 128 (that’s 2^{7}).

Now we are going to subtract 128 from 138. Here is the equation:

**138 – 128 = 10**

Then take that 10 and take a look at the list of exhibitors. The power of 2 that comes closest to 10 is 2.^{3}or 8. So, at this point, our job is to subtract 8 from 10. Like this:

**10 – 8 = 2**

And what do you know? The number 2 is equal to 2^{1}.

This process gave us three important numbers: 128, 8 and 2. Our next goal is to add them up.

**128 + 8 + 2 = 138.**

Find a sheet of paper if you haven’t already. Write the value of each exponent of 2 starting with “128” (remember, it’s 2^{7}) and “1” (which equals 2^{0}). Do *this in descending order from left to right*. And be sure to leave a space between each number.

Your doodle should look like this:

128 64 32 16 8 4 2 1

As you can see, eight individual values are listed here. Draw an arrow pointing down (↓) under each value.

Then check out the addition problem we wrote above, the one that says **128 + 8 + 2 = 138**

Do you see a “128” in this problem? If so, write a “1” under the corresponding arrow. Is there a “64” written in the equation? Nope ! So under this arrow we will write a “0”. Keep the same pattern and you will get this:

Sound familiar? We’re left with 10001010 – and as we’ve already established, that means “138”.

So this is it. Our proverbial magician made the rabbit disappear and brought him back. Carrots all around!

Comments are closed.