How to convert between decimal and binary
May 12th, 2007
How to convert between decimal and binary:
Ground rule: You need to know the ‘magic’ numbers. 1,2,4,8,16,32,64,128,256,512,… (They double) How to convert Dec to Bin: Let’s use 25 as a starting example. The thing to remember is you work the magic numbers from right to left, and the binary string from left to right. Find half of the number, in this case 12.5. Look for the first magic number that is over 12.5, in this case 16. Now we need to start our binary string as we’ll call it. Right now its empty. Since we can use 16, we add ‘1′ to the string, giving us ‘1′ at the moment. Now take 25 and subtract 16, you should get 9. Since we started at 16, the next magic number we remove is 8. Since we can use 8, add another 1 to our binary string, which now should be ‘11′. Subtract 8 from 9 to get one. The next number we try is 4, but 4 can’t be subtracted from 1 so we can’t use it and add a ‘0′ to the binary string, which now is ‘110′. Next we try 2, which causes the same problem as 4. So now our binary string is ‘1100′. Lastly we subtract 1 from 1 to get 0, our end goal. Since we used the 1 we have to add another ‘1′ to the binary string, giving us ‘11001′. That’s it. 11001 is the binary form of 25. If you don’t believe me that it was that simple and it’s correct, go ahead and look, it is. Let’s try another number, a bigger one like 251, since we just mastered converting Dec to Bin. Half of 251 is 125.5, so we start off with the next highest magic number, 128. Since we used 128, we’ll add ‘1′ to our binary string, which is ‘1′ now. Subtract the 128 from 251 to get 123. Now we need to try the next number, 64, which fits, so we add another ‘1′ to our binary string, and subtract 64 from 123 to get 59. Now our binary string is ‘11′. The next number we need to try is 32, which fits, so we add another ‘1′ to our binary string, and subtract 32 from 59 to get 27. The next number to try is 16, which also fits, so again we add ‘1′ to the binary string, and subtract 16 from 27 to get 11. The next number to try is 8, which fits, so we add ‘1′ to the binary string again, and subtract 8 from 11 to get 3. The next number to try is 4, but it doesn’t fit. So we add a ‘0′ to our binary string. The next number to try is 2, which fits, so we add a ‘1′ to our binary string, which should be ‘1111101′ right now. We then subtract the 2 from 3 since it fit and are left with 1. The next number to try is 1, and it fits, so we add another ‘1′ to our binary string, which is now ‘11111011′. Then we subtract 1 from 1 to get 0. If the end result is not 0, you screwed up somewhere. Don’t get too frustrated, your still learning, keep trying.
How to convert Bin to Dec:
Let’s use 11001 as a starting example. The thing to remember is you work the magic numbers from left to right, and the binary string from right to left. It’s the opposite as when going to binary. Don’t forget that. We take the first magic number and multiply it to the ‘first’ binary number (remember, we’re starting on the right end of the binary number, not the left) to get a total of 1, because 1 x 1 = 1. This time we will use an arithmatic string, meaning you’ll add these, not just stick them in a row one after the other. The first number in our atithmatic string is ‘1′. Now we take the next binary digit, 0, and mulitply it by the next magic number, 2. 2 x 0 = 0, so the next number in out arithmatic string is ‘0′. The arithmatic string should now be ‘1 + 0′. Now we take the next binary digit, another 0, and mulitply it by the next magic number, 4. 4 x 0 = 0, so the next number in out arithmatic string is ‘0′. The arithmatic string should now be ‘1 + 0 + 0′. Now we take the next binary digit, 1, and mulitply it by the next magic number, 8. 1 x 8 = 8, so the next number in our arithmatic string is ‘8′. The arithmatic string should now be ‘1 + 0 + 0 + 8′. Now we take the next binary digit, another 1, and mulitply it by the next magic number, 16. 16 x 1 = 16, so the next number in our arithmatic string is 16. The arithmatic string should now be ‘1 + 0 + 0 + 8 + 16′. Now we add the arithmatic string up and get 25. So 25 is the decimal form of 11001. Again, if you don’t believe me that it was that simple and it’s correct, go ahead and look, it is. Let’s try this with a bigger number. We’ll use 11111011. We take the first magic number and multiply it to the first binary number to get a total of 1, because 1 x 1 = 1. Now we add ‘1′ to our arithmatic string, which is ‘1′ right now. Now we take the next binary digit, another 1, and multiply it by the next magic number to get 2, because 2 x 1 = 2. So now we add ‘2′ to our arithmatic string, which is ‘1 + 2′ right now. Then we take the next binary digit, 0, and multiply it by the next magic number, 4, to get 0 because 4 x 0 = 0. So now we add ‘0′ to our arithmatic string, which is ‘1 + 2 + 0′ right now. Then we take the next binary digit, another 0, and multiply it by the next magic number, 8, to get 0 because 8 x 0 = 0. So now we add ‘0′ to our arithmatic string, which is ‘1 + 2 + 0 + 0′ right now. Then we take the next binary digit, 1, and multiply it by the next magic number, 8, to get 8 because 8 x 1 = 8. So now we add ‘8′ to our arithmatic string, which is ‘1 + 2 + 0 + 8′ right now. Then we take the next binary digit, another 1, and multiply it by the next magic number, 16, to get 16 because 16 x 1 = 16. So now we add ‘16′ to our arithmatic string, which is ‘1 + 2 + 0 + 8 + 16′ right now. Then we take the next binary digit, another 1, and multiply it by the next magic number, 32, to get 32 because 32 x 1 = 32. So now we add ‘32′ to our arithmatic string, which is ‘1 + 2 + 0 + 8 + 16 + 32′ right now. Then we take the next binary digit, another 1, and multiply it by the next magic number, 64, to get 64 because 64 x 1 = 64. So now we add ‘64′ to our arithmatic string, which is ‘1 + 2 + 0 + 8 + 16 + 32 + 64′ right now. Then we take the next binary digit, fortunatly our last one too, another 1, and multiply it by the next magic number, 128, to get 128 because 128 x 1 = 128. So now we add ‘128′ to our arithmatic string, which is ‘1 + 2 + 0 + 8 + 16 + 32 + 64 + 128′ right now. We’re finally done. If you add the arithmatic string up you get 251, the decimal form of 11111011. If you didn’t get the right answer, don’t get too frustrated, your still learning, keep trying. Well that’s it. You just learned how to convert decimal to binary, and binary to decimal. Wasn’t it fun? I’ll make a guide on how to do this with Hexadecimal and Octal once I learn how to do those. This guide is written by Joe Moschetti jmoschetti45ATgmailDOTcom.
Entry Filed under: Computers
Leave a Comment
You must be logged in to post a comment. To get a login, either register here or mail me at admin@whoiam55.at.preempted.net
Trackback this post | Subscribe to the comments via RSS Feed