Dan Mahoney ([info]gushi) wrote,
@ 2009-06-29 04:30:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Bored and doing binary math...

So, because I was curious, I decided to calculate, by hand, prime.gushi.org's ip address in decimal. It took me about 10 minutes to do. It took me well over an hour to HTML this entry and draw up and rearrange all the tables, by hand. Maybe you can learn something from it.

prime.gushi.org is: 72.9.101.130.

First step is to reduce each of those to its binary equivalent, as four eight-bit numbers. 8 bits only go up to 255, which might explain to some of you why you could never get more than 255 gems in the original legend of Zelda...

Bit729 101 130
128   X
64X  X  
32   X  
16     
8X X   
4   X  
2    X
1  X X  

That gives us:

01001000 00001001 01100101 10000010 (the colors will become clear in a moment)

Now, for the above, instead of the above table being 8-bits, we translate to their 32 bit values (note that just like in decimal, the numbers on the right are the "low", or "least significant" numbers!

Just like in decimal where you have the "ones place" or the "tens place", in binary you have the "one place (20)" or the "two place (21)", or the "four place (22)" all the way up to the "two trillion, four hundred seventy four million, four hundred eighty three thousand six hundred fourty eight place (231)", which, zero-inclusive, is 32 bits. Astute readers will notice that each column is double the one before it (this is different from decimal), and also that maxing out all the rightmost columns is one less than the value of the next column, so 1000 (8) is one more than 111 (7) in binary: 111 is the max value with three places, just as 1000 is one more than 999 in decimal; 999 is also the max value with 3 places.

It's the same as our number system, once you run out of spaces, you move up one, but each "place" can only count to "one", and just like a "2" in the "hundreds" place means "200" in decimal, a "1" in the "eight" place in binary means "8"

Admittedly, I used a calculator to get all the powers of 2 below:

BitPower of 2Value BitPower of 2Value BitPower of 2Value BitPower of 2Value
 31 2147483648  23 8388608  15 32768 X7 128
X 30 1073741824  22 4194304 X 14 16384  6 64
 29 536970912  21 2097152 X 13 8192  5 32
 28 268435456  20 1048576  12 4096  4 16
X 27 134217728 X 19 524288  11 2048  3 8
 26 67108864  18 262144 X 10 1024  2 4
 25 33554432  17 131072  9 512 X 1 2
 2416777216 X 1665536 X 8256  01

So, we substitute in as above, and then we add up all the numbers with an X above.

In other words, this IP address is the same as saying:
230 + 227 + 219 + 216 + 214 + 213 + 210 + 28 + 27 + 21 OR:

(commas added for readability only)
1,073,741,824 + 134,217,728 + 524,288 + 65,536 + 16,384 + 8,192 + 1,024 + 256 + 128 + 2 OR:

1208575362. Yeah, that's the "real" address there.

We try to open that in a browser (and yes, browsers recognize decimal IP addresses), and...http://1208575362/there you go. The funny thing is, all the above seems complicated, but computers do it in a split second. After all, when you type something in a browser, your computer doesn't connect to a name, or even a dotted-quad IP address, it makes a connection right to the integer...all the dotted quads are just for us foolish humans.

Let's think of another one. I will guess the IP address of your computer, and post it here in binary for the world to see: 01111111 00000000 00000000 00000001

There you go, decode it, to either its dotted-quad (use the top table) or binary (use the bottom one) equivalent!

So, anyone want to try it with ipv6 addresses? They have 128 bits as opposed to the mundane 32 here, and translating them means learning to count up to F!




(15 comments) - (Post a new comment)


[info]featheredfrog
2009-06-29 10:26 am UTC (link)
:D

Next tutorial, using perl's "pack" and "unpack" to translate between forms of an IP address.

(Reply to this)


[info]afterannabel
2009-06-29 10:33 am UTC (link)
Admittedly, I used a calculator to get all the powers of 2 below
Cheater. :P

(Reply to this) (Thread)


[info]gushi
2009-06-29 10:39 am UTC (link)
It's easier in binary:

Double 10000 = 100000

(Reply to this) (Parent)(Thread)


[info]gushi
2009-06-29 10:48 am UTC (link)
Double 111 = 1110...Double 101 = 1010, double 101010 = 1010100, four times 111 = 11100. Doubling is as simple as adding a zero in base 2, try it!

(Reply to this) (Parent)


[info]cubbi
2009-06-29 10:50 am UTC (link)
Counting up to F makes it easier, because the first step (converting to binary) becomes trivial.

(Reply to this)


[info]mr_cellaneous
2009-06-29 02:26 pm UTC (link)
Ah, but did you remember to convert to network byte order?

(Reply to this) (Thread)


[info]gushi
2009-06-29 09:36 pm UTC (link)
Not something I normally think about, as it's relatively standard math in dotted-quad notation. I'd also say it's out of scope for this particular exercise. However, for people who wonder what you're talking about: In general, this, and specifically, the conversion function mentioned here :)

(Reply to this) (Parent)(Thread)


[info]mr_cellaneous
2009-06-29 10:43 pm UTC (link)
I was joking anyway, because network byte order is what you were already working in, unless you have an intel processor for a brain. :)

(Reply to this) (Parent)

You can do this without binary
[info]doctorbinary.myid.net
2009-06-29 08:22 pm UTC (link)
I love binary as much as the next guy, but you can do this without binary: 72.9.101.130 = 72 * 2^24 + 9 * 2^16 + 101 * 2^8 + 130 = 1208575362 . In essence, you're treating the IP address as a base 256 number: 72 * (2^8)^3 + 9 * (2^8)^2 + 101 * (2^8)^1 + 130 * (2^8)^0. And by the way, you could look up powers of two in a table instead of using a calculator (for example, see mine at http://www.exploringbinary.com/a-table-of-nonnegative-powers-of-two/ ).

(Reply to this) (Thread)

Re: You can do this without binary
[info]gushi
2009-06-29 09:39 pm UTC (link)
I think we have different focuses here, and I think the binary in-between makes it a lot easier to understand other things, like subnets, masking, and network classes.

Good link, tho.

(Reply to this) (Parent)(Thread)

Re: You can do this without binary
(Anonymous)
2009-06-29 11:16 pm UTC (link)
Don't get me wrong, I like the binary analysis. But since you wanted to go from dotted decimal to decimal, I skipped the binary step (although the binary step adds insight into why my "base 256" approach works).

(Reply to this) (Parent)(Thread)

Re: You can do this without binary
[info]gushi
2009-06-30 01:57 am UTC (link)
FWIW, the <sup> tag works wonders in clarifying those littleexponents.

(Reply to this) (Parent)(Thread)

Re: You can do this without binary
(Anonymous)
2009-06-30 02:30 am UTC (link)
I didn't realize livejournal lets you enter html in comments (unlike WordPress)! This will look better:

72*(28)3 + 9*(28)2 + 101*(28)1 + 130*(28)0

(Reply to this) (Parent)(Thread)

Re: You can do this!
[info]cozycabbage
2009-07-01 01:34 am UTC (link)
Actually, I think Wordpress allows it, though the set of tags is usually limited to such things as <em> and such.

(Reply to this) (Parent)


[info]otakon39102
2009-06-30 08:13 am UTC (link)
There's no place like 127.0.0.1!

(Reply to this)


(15 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…