
Converting Binary to Decimal
January 26, 2008I currently work at a job that often leaves me time with nothing to do, and I’m not allowed to read a book or browse the Internet or anything like that. I have two options for these times: I can drag my work out so that something that would normally take 5 minutes takes me an hour, or I can do things that aren’t productive but at least look like I’m doing something productive to the casual onlooker.
Well, I like to get things done as efficiently and as quick as possible, so the first option doesn’t sit well with me most of the time. That leaves me with the second option, and one of my favorite pastimes for these situations is converting binary numbers to decimal and vice-versa. Here’s how it works:
The decimal system counts numbers based on ten separate digits, 0-9. Once you get to 9, the next number is 10. In other words, you increase the 10’s column by one and reduce the 1’s column back to 0. Okay, we all know that.
The binary system counts numbers based on only two digits, 0 and 1. So, you start out with 1, and then since there is no 2 to go to, the next number is 10. It’s the same thing as decimal, you increase the 10’s column by one and reduce the 1’s column back to 0. Therefore, counting would go something like this: 1, 10, 11, 100, 101, 110, 111, etc.
As you can see, binary 1 is equal to decimal 1. Binary 10 is equal to decimal 2. Binary 100 is equal to decimal 4.
So, how do you convert binary to decimal then? Well, if you notice above, 1=1, 10=2, 100=4, and if you continue that pattern, then 1000=8, 10000=16, 100000=32, 1000000=64, 10000000=128, and so on.
Now let’s say you wanted to get the decimal value of 01011001. What you want to do is add up all the values for each column, so we get: 1000000 (64) + 10000 (16) + 1000 (8) + 1 (1) = 89.
Here’s a table to make it easier to visualize:
|
Binary |
10000000 | 1000000 | 100000 | 10000 | 1000 | 100 | 10 | 1 |
|
Decimal |
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
Now, to convert from decimal to binary. Let’s take the number 173 for example. To make this conversion, think back to the table. The highest value on the table that fits into 173 is 128. Now subtract 128 from 173 and add 1000 0000 to our binary number. So now we are left with 45 in decimal and 1000 0000 in binary.
(Note that you can put spaces between every four digits in binary to make it easier to read the same way you would put commas between every three digits in decimal.)
Next, 64 doesn’t fit into 45, but 32 does, so we subtract 32 from 45 and add 10 0000 to our binary number, leaving us with 13 in decimal and 1010 0000 in binary.
The next number that fits into 13 is 8, so subtract from decimal, add to binary, and we are left with 5 and 1010 1000.
Next, with our decimal number 5, the next binary number that fits in is 4, so we are left with 1 in decimal and 1010 1100 in binary.
Finally, 1 fits nicely into 1, so our final number is 1010 1101. Tada!
Pretty simple, right? A good way to check your answers is to use a calculator application like the one in Windows under Start>All Programs>Accessories>Calculator. Go to the “view” menu and change the mode to scientific. Then you’ll notice in the upper left part of the calculator, there are four radio buttons: Hex, Dec, Oct, and Bin. These stand for Hexadecimal, Decimal, Octal, and Binary. The default is Decimal, and you can change to any of the other modes by clicking on the corresponding radio button. So, to check your answer for the above problem, enter 173 in Decimal mode, and then click on the Bin radio button, and it will come up with 1010 1101. The same as what we got! Good job!
Go ahead and practice next time you’re bored in class or at work. If anyone walks by and sees the piece of paper in front of you filled with ones and zeroes and gives you a strange look, just say, “I am a robot, leave me alone!”
Now, you may ask if this has any practical use. Well, any computer scientist or network engineer can tell you that this is very important when dealing with computers and networks. Why? Because digital signals are sent using electrical pulses and there are only two kinds of signals: on and off. Using binary, 1 represents on, and 0 represents off. In a computer, one of these on or off signals is referred to as a bit, and eight bits equal one byte. Sound familiar? When you start getting into computers you see how this presents itself all over the place.
For example, IP addresses. The number set 209.85.173.103 is the IP address for www.google.com. (Try putting http://209.85.173.103 into the address bar of your browser and see what comes up) An IP address is made up of four numbers that range from 0 to 255. Hmm… Each of these numbers is referred to as an octet. Hmm… Well, if you didn’t catch the connection, an IP address is refered to as an octet because if you convert it to binary, each of the numbers has 8 digits. So from 0 to 255, that’s everything from 00000000 to 11111111. You’ll never see an IP address with a number higher than 255 in it. So, if we convert Google’s IP address to binary, we get 11010001.01010101.10101101.01100111. Pretty cool, huh?
Anyway, that, in a nutshell, is how to convert binary to decimal and vice-versa. And that, in a nutshell, is how I spend my time when I’m bored at work.
I think I’m going to have to add this to my list of hobbies.
Sweet! The more the merrier!
you should make a table of the binary to decimal
Thanks for the comment paul. I’m not exactly sure what kind of table you are talking about. Did you mean one that is lists something like decimal 1 = binary 1, decimal 2 = binary 10, decimal 3 = binary 11, etc. etc., or did you have something else in mind? I’d be glad to post anything that would make it easier to understand.
omg.. good work, dude
Hate to break it to you but i just coded a php script to do it.
looks like all you time wasting is over… haha
http://binary2ip.x10hosting.com/
Sweet! Even better!
thanks man now i understand binary to decimal & vice versa