Recently I passed the ICND1 exam. The key to successfully passing this exam is the ability to quickly calculate IPv4 subnetting.

### Binary numbers

Before we begin, we have to learn the binary numbers.

2^0 = 1
2^1 = 2
2^2 = 2 * 2 = 4
2^3 = 2 * 2 * 2 = 8
2^4 = 2 * 2 * 2 * 2 = 16

Learn these numbers: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, ….

### Convert a decimal number to binary quickly

First convert the decimal number as a sum of those numbers, that we have just learnt. Then write 1’s and 0’s from right to left. Write 1 if the sum contains the number, write 0 otherwise.

For example convert 13.

13 is less than 16, but greater than 8. Subtract 8 from 13. 13 – 8 = 5.

5 is less than 8, but greater than 4. Subtract 4 from 5. 5 – 4 = 1.

13 = 8 + 4 + 1.

Now write numbers from right to left. 1 is in the sum, so write 1. 2 is not in the sum, so write 0. 4 is in the sum, so as the 8. Write 1 and 1. The binary form of 13 is 1101.

Let us convert 243. 243 is less than 256,  but greater than 128. 243 – 128 = 115.

115 is less than 128, but greater than 64. 115 – 64 = 51.

The rest is: 51 – 32 = 19.

19 – 16 = 3.

3 – 2 = 1.

243 = 128 + 64 + 32 + 16 + 2 + 1.

Now write 1’s and 0’s. 243 = 11110011

IPv4 address contains four octets. Each octet is between 0 and 255. For example 192.168.10.1 is an IPv4 address. The left part is the network address, the right part is the host address. The subnet mask tells where the separation is.

The subnet mask has the same form. The left part contains only 1’s, the right part only contains 0’s. For example 255.255.255.0 is a valid subnet mask, because we can write it as: 11111111.11111111.11111111.00000000. The left 24 bit is 1, the right 8 bit is 0. The size of the whole subnet mask is 32 bit. The other form of the subnet mask is /24, which means, the 24 leftmost bit is 1.

192.168.10.1/24 is the same as 192.168.10.1 with subnet mask 255.255.255.0. If we convert the IP address into binary form and perform a logical AND operation with the subnet mask, then we get the network address. The rest is the host address. The broadcast address can be calculated, if we set 1 in each bit in the host address.

11000000.10101000.00001010.00000001 = 192.168.10.1
11111111.11111111.11111111.00000000 = 255.255.255.0, or in other form /24
11000000.10101000.00001010.00000000 = 192.168.10.0, the network address
00000000.00000000.00000000.00000001 = 0.0.0.1, the host address.

Sometimes we have to convert between the two subnet mask form. Let us say the subnet mask is /27. What is the other form? 27 can be written as 8+8+8+3. The subnet mask is 11111111.11111111.11111111.11100000. The first three octet is obviously 255. The last one is 11100000. In decimal format 128*1+64*1+32*1+…=128+64+32=224. The subnet mask is 255.255.255.224. We can learn the following seven numbers:

128, 192, 224, 240, 248, 252, 254

128 = 128
128+64 = 192
128+64+32 = 224
128+64+32+16 = 240
128+64+32+16+8 = 248
128+64+32+16+8+4 = 252
128+64+32+16+8+4+2 = 254

But there is a faster method.

32-27 = 5, these are the number of the zero bits in the subnet mask.

2^5 = 2*2*2*2*2 = 32

256 – 32 = 224

Let us do another example, the subnet mask is /29. What is the another form of subnet mask?

32-29 = 3

2^3 = 2*2*2 = 8

256 – 8 = 248

Another example, but this time we operate on the third octet instead of the fourth. The subnet mask is / 18.

32-18 = 14 = 8 + 6

The subnet mask is 11111111.11111111.11000000.00000000. We calculate everything the same way, but we work on the third octet. So the number of the zero bits in the third octet is 6.

2^6 = 64 (I calculate it on my fingers. 2, 4, 8, 16, 32, 64)

256-64 = 192

What is the /x form of subnet mask if the subnet mask is 255.255.255.252?

256-252 (the last octet) = 4

2^2 = 4, the number of the zero bits in the subnet mask is 2.

32-2 = 30, the whole size of the subnet mask is 32 bit, and the number of zero bits is 2, thus the subnet mask is /30.

### Subnet calculation

First we convert the subnet mask as the /x form is more appropriate for such calculations.

256-240 = 16

2^4 = 16, the subnet mask contains 4 zeros on the right. (32-4 = 28, thus the IP can be written 110.43.27.118/28).

118 = 64+32+16+4+2 = 01110110 in binary format.

The last four bit should be removed. Actually the subnet mask is X.X.X.11110000, and the IP is Y.Y.Y.01110110. The network address is Y.Y.Y.01110000.

01110000 = 64+32+16 = 112

Thus the network address is 110.43.27.112.

Again there is a faster way. Before I explain it, let us discuss the subnetting.

If the subnet mask is 255.255.255.0, the range is 0-255, as we have 8 bits for the hosts. The first address is the network address, the last is the broadcast address. The number of the hosts is 2^8-2 = 254

If the subnet mask is 255.255.255.128, the range is 0-127 and 128-255. We have 7 bits for the hosts and 1 bit for the networks. We divided the range into two subnets. The number of the hosts in each subnet is 2^7-2 = 126.

If the subnet mask is 255.255.255.192, the range is 0-63, 64-127, 128-191 and 192-255. We have 6 bits for the hosts and 2 bits for the networks. We divided the range into four subnets. The number of the hosts in each subnet is 2^6-2 = 62.

If we need for example 43 host in a subnet, we need (2, 4, 8, 16, 32, 64) 6 bits for the hosts. (255.255.255.11000000 subnet mask). The appropriate subnet mask is (2^6 = 64, 256-64 = 192) 255.255.255.192. If we have an IP address 192.168.10.0/24 and we divide it into four subnet with such netmask, we have 64 available address in each subnet (do not forget, the network and broadcast address are not valid addresses). The start of the ranges are:

192.168.10.0
192.168.10.64
192.168.10.128
192.168.10.192.

Notice the pattern. The subnet mask is 255.255.255.192. 256-192 = 64. 64 is the size of the range. The next range is +64 away from the previous one.

Let us go back to our original question: What is the network address of the following IP address 110.43.27.118 (subnet mask 255.255.255.240)?

256-240 = 16, this is the size of the range.

110.43.27.0
110.43.27.16
110.43.27.32
110.43.27.48
110.43.27.64
110.43.27.80
110.43.27.96
110.43.27.112
110.43.27.128

118 is between 112 and 128. Thus the network address of 110.43.27.118 is 110.43.27.112, the broadcast address is 110.43.27.127. The broadcast address is always the next to the last address in the range (Get the next network address, which is 110.43.27.128, minus 1).

If you need 300 hosts in a network, then you need 9 bits. 9 bit is 512 (2, 4, 8, 16, 32, 64, 128, 256, 512). The fourth octet will be zero, 8 zero bits. We need 1 bit from the third octet. The subnet mask will be 255.255.11111110.0. This can be written as 255.255.254.0.