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.

Advertisement

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!

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."

Advertisement

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 2^{3}, 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.

2 = 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^{10} = 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.

Advertisement

In any binary number, the bit that's farthest to the right has to be multiplied by 2. Then the one to its immediate left is multiplied by 2^{1}.

Next, the bit to its left is multiplied by 2^{2}. 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:

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 2? The correct answer is 0.

One problem down, seven to go. Move one space to the left. See the "1" there? Well, 1 x 2^{1} = 2. Now move yet another space to the left. Doing so will give you 0 x 2^{2}, 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 2 = 0

1 x 2^{1} = 2

0 x 2^{2} = 0

1 x 2^{3} = 8

0 x 2^{4} = 0

0 x 2^{5} = 0

0 x 2^{6} = 0

1 x 2^{7} = 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.

Advertisement

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 2^{8}) and 128 (that's 2^{7}).

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 2^{3}, 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 2^{1}.

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 2^{7}) and "1" (which equals 2). 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:

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!

Now That's Funny

The popular sci-fi comedy series "Futurama" loves itself some math jokes. In the season two episode "The Honking," a haunted mansion is willed to the rascally robot, Bender Bending Rodriguez. When he goes inside, he's terrified to discover a secret binary message that reads "1010011010." Maybe Bender was right to be scared; in decimal form, that translates to "666."

Advertisement

Cite This!

Please copy/paste the following text to properly cite this HowStuffWorks.com article: