Week 5 Posting - Converting Decimal IPs to Binary

Hello everyone and welcome to week 5. I'm generally good at math, but the concept of converting base 10 numbers into base 2 numbers has given me a headache this week.


Ouch. Source.


The readily available conversion calculators and charts didn't do much to ease my mind either. Maybe it's because "show your work" was drilled into my head in almost every math class I've taken, but I need to know how these conversions to make them work for me, even if I will rarely, if ever, do the long form conversion in day to day work. I figure if I explain it in writing, it will help cement it in my mind.

Turns out, it's pretty easy.

Binary numbers are also known as base 2 numbers, and decimal numbers are what we normally use to count. Combining the two into eight bit binary values without a calculator is not nearly as complicated as it sounds.

Using 8 bits, we can convert decimal numbers 0 - 255, or (2^8) - 1. For each bit, there is a power of two.

So from left to right, or largest to smallest:

|2^7|2^6|2^5|2^4|2^3|2^2|2^1|2^0|
|128| 64 | 32 | 16|  8  |  4  |  2  |  1  |


From here, we can convert any number between 0 and 255 to an 8 bit binary number by using
subtraction.

For example, let's say we want to figure out what 160 is in 8 bit binary. Once again we work left to right.

The question we ask with each bit is "does this power of two go into this number?" If the answer is yes, then that bit is a 1, if the answer is no, that bit is a 0.

Does 128 go into 160? Yes. So the first bit is 1.

To move forward, we subtract 128 from 160. 160 - 128 = 32. We repeat the earlier process. Does 64 go into 32? No, so we put a 0 for the second bit and do not subtract. Obviously 32 goes into 32, so the third bit is 1 and we subtract 32 from 32, which equals zero. No other numbers will go into zero, so the fourth through eighth bits are all 0s.

So, 160 in 8 bit binary is: 10100000

I hope explaining the conversion process has made this concept a little easier to grasp for you, I know it has for me. See you all next week!



Comments

Popular Posts