How to Convert Binary Into Decimal (and Vice Versa)

By: Mark Mancini  | 

binary code
Binary code tells your computer how a podcaster’s voice sounds, which colors should appear in a YouTube video and how many letters were used in that email your boss just sent. Jan Hakan Dahlstrom/Getty Images

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

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

But your computer takes another approach.

Laptops, smartphones and other devices rely on binary code. A mathematical language, binary relays instructions to these high-tech gizmos. It tells your computer how a podcaster's voice sounds, which colors should appear in a YouTube video and how many letters were used in that email your boss just sent.

You know. Important stuff like that.

Binary code lives up to its name. Unlike the decimal system, it only uses two types of digits, which programmers call "bits." Usually, there's "0" and there's "1." And that's all.

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

Don't worry. With a little patience, you can master this "trick" — and have fun doing it!

Advertisement

Know Your Exponents

Technically, 0 and 1 are the only bits you need to write binary numbers. But to make sense of them, you have to understand a third value: 2.

It's best if we explain this by way of example. The number 138 is properly expressed in binary code as "10001010."

How can your computer tell this apparent string of gibberish means "138"?

Programming is part of the answer. Somebody has informed your device that — in this case — the binary code spells out a number instead of a written word or sentence; there's a separate method for decoding the latter.

Once this basic fact is established, the code works by assigning a different exponent of 2 to each individual bit (i.e., every 0 and every 1).

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

Please enjoy the following powers of 2 list. Trust us, you'll want to look this over soon.

20 = 1

21 = 2

22 = 4

23 = 8

24 = 16

25 = 32

26 = 64

27 = 128

28 = 256

29 = 512

210 = 1024

Advertisement

Converting Binary to Decimal

Now let's go back to our original binary number: 10001010

If English is your native language, brace yourself. You're about to fight against your instincts. See, written English is read from left to right. But now, we have to break down that binary number by going in the opposite direction: from right to left.

In any binary number, the bit that's farthest to the right has to be multiplied by 20. Then the one to its immediate left is multiplied by 21.

Next, the bit to its left is multiplied by 22. And so on and so forth.

Notice a pattern here? The individual exponents of 2 are being used in ascending order, from right to left.

OK, so now our job is to keep that pattern going until we've matched an exponent of 2 to every bit — every single 0 and 1— in the binary number.

We'll stop once the final bit, the one on the far left side, has been multiplied by the appropriate exponent of 2.

A helpful way to keep your figures straight is to physically line up the exponents over their corresponding binary bits on a sheet of paper. Ideally, it should look something like this:

Binary to decimal
Binary to decimal
HowStuffWorks

Good stuff. Alright, now let's return to the actual conversion.

Because 10001010 contains 8 individual bits, we're going to do 8 separate multiplication problems. Let's begin with the 0 on the far right end.

What's 0 x 20? The correct answer is 0.

One problem down, seven to go. Move one space to the left. See the "1" there? Well, 1 x 21 = 2. Now move yet another space to the left. Doing so will give you 0 x 22, which equals 0.

If you continue using this pattern, proceeding from the farthermost right to the farthermost left, here's what you'll ascertain:

0 x 20 = 0

1 x 21 = 2

0 x 22 = 0

1 x 23 = 8

0 x 24 = 0

0 x 25 = 0

0 x 26 = 0

1 x 27 = 128

Hang on, we're almost at the finish line!

Take the results of all those multiplication problems and add them together. Not multiply, add. Savvy?

What does 0 + 2 + 0 + 8 + 0 + 0 + 0 + 128 equal?

Before we answer that, let's get rid of all those zeroes. We don't need them 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've come full circle! Go take a victory lap.

Note that 138 is a whole number. There is a technique for converting numbers with a fractional component, like 0.25 and 3.14, into binary. But full disclosure: It's kind of complicated. If that doesn't bother you and you'd like 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 the conversions.

Advertisement

Converting Decimal to Binary

Having changed "10001010" into "138," it's time to reverse our process. Suppose you'd started out with 138 and had to convert it to binary. How would you do it?

Once again, exponents are the key to the whole shebang.

Have another look at our "powers of 2" list. Find the value that comes the closest to 138 without exceeding it.

A quick reread tells us 138 sits between 256 (which is 28) and 128 (that's 27).

Now, we're going to subtract 128 from 138. Here's the equation:

138 - 128 = 10

Next, take that 10 and have a second gander at the exponent list. The power of 2 which comes the closest to equaling 10 is 23, or 8. So at this point, our job is to subtract 8 from 10. Like so:

10 - 8 = 2

And what do you know? The number 2 is equal to 21.

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

128 + 8 + 2 = 138.

Find a piece of paper if you haven't already done so. Write out the value of every exponent of 2 beginning with "128" (remember, that's 27) and "1" (which equals 20). Do this in descending order from left to right. And make sure to leave some space between each number.

Your scribbling should look like this:

128 64 32 16 8 4 2 1

As you can see, there are eight individual values listed here. Draw a downward-pointing arrow (↓) below each value.

Then, consult the addition problem we wrote down up above, the one that says 128 + 8 + 2 = 138

Do you see a "128" in that problem? If so, write a "1" underneath the corresponding arrow. Is there a "64" written in the equation? Nope! So beneath that arrow, we're going to write a "0." Keep to the same pattern and you'll get this:

binary to decimal conversion
Our decimal to binary conversion.
HowStuffWorks

Look familiar? We're left with 10001010 — and as we've already established, that means "138."

So there you go. Our proverbial magician has made the rabbit disappear and brought it back again. Carrots all around!

Advertisement

Featured

Advertisement

Loading...