TCP/IP - The Math Behind It All

Everyone has heard the expressions tcp/ip address, or network address, and subnet mask used in reference to configuring your computer to either get on the Internet, or connect to your network at work. What does this mean? I knew what numbers to put into a machine to make it work long before I knew why those numbers worked. To ever really understand sending data and packets on the Internet, and how to properly set up your own network, it is important to understand not only what the valid numbers are, but how they are calculated, and physically what they mean. To understand what I mean when I write 216.227.108.198/24, or use a command in Linux like:

ifconfig eth0 192.168.7.7 netmask 255.255.255.0

Where in the world do those numbers come from, and what do they mean?

First, let us talk about binary math. Everyone has heard of it, but what does it mean and do? Computers work off of electricity (no joke!), and in the world of electrons and electricity there are only two states, charged and uncharged. This corresponds to either a zero (0), uncharged, or a one (1), charged. So computer chips naturally think and work in binary terms. This is why all the numerical representations of address must be expressed in binary form. Below is a table that shows some simple counting and how the numbers correspond to their binary equivalent.

Table 1-3. Binary to decimal translations

00000001= 110000000= 128
00000010= 211000000= 192
00000011= 311100000= 224
00000100= 411110000= 240
00000101= 511111000= 248
00000110= 611111100= 252
00000111= 711111110= 254
00001000= 811111111= 255
00001001= 901111111= 127
00001010= 10  

Now why did I use 8 numbers to represent each of these numbers? because IP addresses are represented by 4 octets(ie. 8 bits or 1 byte), which corresponds to 8 binary digits in each octet for a total of 32 bits. So an ip address is considered a 32 bit number. Rather than us to always write out, or type in a bunch of binary numbers into the computer to set the addresses and subnet masks, we use the decimal equivalent. So if you are trying to figure out what the decimal equivalent of a binary number, the equation to use to accomplish this is:

                         (n-1)
        summation of {m*2     }

So a number of 11001001 in binary equals    2^7 + 2^6 + 2^3 + 2^0 =
                                         =  128 + 64 + 8 + 1 
                                         =  201

This may seem really strange, but if you practice it a little, it becomes quite easy to calculate.

The mathematical AND operator

Now what am I talking about when I say AND mathematically? Everyone knows what add means, and what subtract means, but what is a mathematical and and a mathematical or mean? AND is the command that mathematically TCP/IP uses to mask out bits of an address. Look at the following table to see what anding two numbers in binary means.

                1 AND 1 = 1
                1 AND 0 = 0 
    

so 1 AND any number equals that number.

                0 AND 1 = 0
                0 AND 0 = 0

so 0 and any number equals 0. Therefore any number you put a subnet mask of 255 against ( 255=11111111) you get that number. Any type you use a 0 as a subnet mask, you get a zero. For instance:

        ip address of   192.168.13.0
        subnet mask of  255.255.0.0

What is the network address? We AND these two numbers together and get 192.168.0.0 is the network's address. Write these numbers down in binary and look at them and you will see how the ones and zeros mask portions of the address.