Ever wondered what an IP address is, and how they came to be? After you’ve fully read and understand the topics I’m about to discuss, IP addresses won’t be a mystery to you anymore.

I will be covering IP version 4 (IPv4) addresses in this blog entry.

## Counting in binary

As almost everything related to how a computer works, IP addresses use the binary numbering system. Let’s first dig into how that works, before we continue to delve deeper into how IP addresses work.

Have you ever given thought to how we count and use numbers? There are a lot more numbering systems than the one we currently use. However, the numbering system we use is called “Base 10”. Our counting system doesn’t need any explanation, but here it goes anyway.

We start with a zero and when we reach the last number, we add a one on the left side and start with a zero again. Like this:

```0
1
2
3
..
10
11
12
13
..
20
21
22
23
..
100
101
102
103
..
1000
1001
1002
1003
etcetera..```

That’s not something new. But digital circuitry and also IPv4 addresses, use the “Base 2” system. This means that the regular system of 0 through 9 with “Base 10” is not possible. With “Base 2”, only a 0 and 1 are possible. Like this:

```0
1
10
11
100
101
111
1000
1001
1010
1011
etcetera```

But where does the use of this system come from? Why not use our “normal” numbering system? Let me explain:

Our modern computers and devices that use digital circuitry convey information by using electricity. When a current passes, it is read as a one, and when next to no current passes, it is read as a zero. This is basically a TRUE or FALSE system. If we were to use a system that would support more than 2 states, let’s say a Base 3 system, 0, 1, 2,  it would need to tell the difference by the amount of voltage which passes through the system. Let’s take, for example, a system that uses 5V maximum. Using 0V would be read as a zero, while using 5V would be read as a 2, which leaves the one. This could be read at 2,5V. The problem with this is that electric signals degrade over distance. The 2,5V could be interpreted wrongfully when sent over a long distance. It might be interpreted as a zero, if the voltage is too low at arrival. This will of course result in wrong calculations, which would be disastrous.

Using a system of only zero and one has been proven to be the most error proof. Currently, there are products which use another numbering system than binary, for example solid state disks, but they are in the minority in comparison to the binary system. Mostly because of the lack of reliability by using anything other than binary.

## How to use IP addresses

So, leaving that short introduction of binary behind us, we now know that IP addresses use the binary system. Why is it then, that IP addresses are written with numbers 0 through 9? This is only to make it readable for us. In actual, IP addresses are also stored in binary systems. Let’s take the following IP address as an example:

`213.136.78.9`

In binary, this IP address is written as follows:

`11010101.10001000.01001110.00001001`

An IP address consists of 4 octets, which each contains 8 bits of information. In total that makes 32 bits. Therefore, an IP address holds 32 bits of information. If we take one octet, we can convert the binary numbers to our base 10 numbering system by using a system of powers.

So let’s convert `00001001` to base 10:

If we begin from the right to the left, you can write the first bit as 20 and the second as 21, continuing all the way to the left and finish with 27. The following table shows how that looks:

27
26 25 24 23 22 21 20
0 0 0 0 1 0 0 1

When we then write the answers to the powers, we get the following:

128
64 32 16 8 4 2 1
0 0 0 0 1 0 0 1

Now it is just a matter of adding up the numbers on the positions where there are 1’s. In this case the answer to the conversion is 9.

If all positions were filled with 1’s, we can calculate the maximum value by adding up all the numbers, which would be 255. This is the maximum number that an IPv4 address can hold, when converted to the base 10 numbering system.

So, now that we know how to convert from binary to base 10, I can now go ahead and continue my explanation of how and why we use IP addresses.

IP addresses are unique identifiers, given to systems that communicate with each other using the TCP/IP protocol. In my previous blog post, I explained about what TCP/IP is, and also briefly touched upon IP addresses. If you want to read up on that, click here: Basic Network Terminology and Operation.

In order to communicate with another system, IP addresses need to be paired with a subnet mask. A subnet mask also uses binary numbers, and is used to tell what the size of the network is. Systems need to know of what network they are part of, in order to communicate with each other. The way systems send information will be different if they’re part of the same network, in comparison to if they are not. This is also covered in my previously mentioned post.

So, let’s explain how subnet masks look and how to are used in conjunction with IP addresses. This is an example subnet mask:

`255.255.255.0`

Let’s now use the example IP address of `192.168.1.0` and elaborate on how these numbers are used to tell the size of the network. The best way to go about this, is to convert the IP address and the subnet mask back to binary. This will look as follows:

```11000000.10101000.00000001.00000000
11111111.11111111.11111111.00000000```

The first line is the IP address, which leaves the second to be the subnet mask. The positions where the subnet mask contains 1’s, are positions where no change is to be made to the ip address. We call this the network portion of the ip address. The positions that use 0’s in the subnet mask, can be used by hosts on a network. We call this the hosts portion of the ip address.

In this case, we have a whole octet to calculate the size of the subnet with. The maximum number in one octet is 255. This is done by changing all the 0’s of the octet to 1’s. By looking at the table of powers, we can calculate this by adding up 128 + 64+ 32 + 16 + 8 + 4 + 2+ 1.

The highest number in a subnet, is used as the broadcast address. The lowest number is used as the network address, which in turn is basically used to identify the network. The network address is generally not used for hosts, although it can be. Anything between the network address and the broadcast address, can be used for hosts.

For this example, we have the following information:

192.168.1.0  192.168.1.1  192.168.1.254  192.168.1.255

Now lets teach you a trick in order to calculate the size of networks from memory, instead of having to first convert to binary in order to get the answer.

Let’s take the following example network:

If we convert this to binary, we get the following:

```10101100.00010000.00100000.00001000
11111111.11111111.11111111.11111000```

If we look at the boundary of the network part and the host part, we see that this is located within the 4th octet. Let’s use the decimal equivalents and line them up:

128
64 32 16 8 4 2 1
1 1 1 1 1 0 0 0

We see here that the subnet mask stops at the value of 8. We can use this information to assume that the next network will start when we increment the current network with 8. Remember, we’re in the 4th octet now. So we will now say that the next network will be 172.16.32.16. We can calculate if this is correct.

The have 3 0’s in the binary equivalent of this hosts part of this network. With three 0’s we can make the following combinations in this octet:

00001 000 = 8
00001 001 = 9
00001 010 = 10
00001 011 = 11
00001 100 = 12
00001 101 = 13
00001 110 = 14
00001 111 = 15

We were calculating with the host part of the binary ip address. The first answer is the network address and the last one is the broadcast address. This leaves us with:

172.16.32.8 172.16.32.9 172.16.32.14 172.16.32.15

Now add one bit to the network portion of the IP address, and we can now definitively say that 172.16.32.16 is the network address of the next subnet.

In the above example, I gave the network address to begin with. What if I give you a host address, instead of the network address and then a subnet mask. How can you quickly tell what network it is part of?

One more then:

Let’s immediately get the value of the last bit in the subnet mask.

`11111111.11111111.11111110.00000000`

Using the conversion table:

128
64 32 16 8 4 2 1
1 1 1 1 1 1 1 0

The value of the last bit in the subnet mask is 2. So the next network can be answered by incrementing this network by 2. We currently have 201 in the third octet. This is an uneven number and is not an increment of 2. If we increment 2 until we reach our network numbers, we get 10.123.200.0 255.255.254.0 and 10.123.202.0 255.255.254.0.

To quickly get the first host, just add 1 to the network address. To get the last host, subtract 2 from the next network. To get the broadcast address of our network, subtract 1 from the next network. The answer in this case is that 10.123.201.20 is a host address in the network 10.123.200.0 255.255.254.0

In order to quickly read IP address from memory, it is key to remember the binary equivalent values for the different subnet mask notations, and from there quickly get the network increments. Here is the list for your convenience:

128  128
192  64
224  32
240  16
248  8
252  4
254  2
255  1

## Classless Inter-domain Routing

Instead of writing 255.255.255.0 for example, we can also write /24. This is called CIDR notation. Classless Inter-domain Routing is actually a whole adressing strategy invented because of the exhaustion of public IPv4 addresses. With this address stategy, they developed a clever way to notate subnet masks.

The concept is quite simple: in the binary equivalent of 255.255.255.0, there are 24 bits of a total of 32 bits which are set to 1’s. This makes the /24.

In the binary equivalent of the subnet mask 255.255.192.0, 18 bits are set to 1, which makes the CIDR notation /18. So, if you follow an IP address with a CIDR notation, you then also know what the subnet mask is.

Thanks for reading, I hope you enjoyed it and learned something by doing so.