How to subnet CIDR blocks

  1. Introduction
  2. Obtaining subnets
    1. First subnet
    2. Second subnet
  3. Conclusion

Introduction

I won’t explain the theory here, I am going to show how to subnet a Classless Inter-Domain Routing (CIDR) block based on an exercise.

Let us say this is the CIDR block 172.31.0.0/16 and we have to create subnets inside.

First, with 172.31.0.0/16, we have this address range = [172.31.0.0 – 172.31.255.255] and we should get a subset of this.

I hope my dear reader that you can see that above range [172.31.0.0 – 172.31.255.255] means a total of 256ร—256=28ร—28=216=65536256 \times 256=2^8 \times 2^8 = 2^{16}=65536 addresses and I know that I promised that no theory would be needed, but just a little bit is going to be mentioned below.

An IP address, to be specific an IPv4 address is composed by 4 octets (8 bits), which means 32 bits in total. For example, let’s see below the same IP represented in two different ways the decimal notation and the binary representation where we can see the 4 octets

IP in decimal notation: 172.31.0.0

IP in binary notation: 10101100.00011111.00000000.00000000

The CIDR block, this tells us the number of bits that will be used for network addresses and the number of bits that will be used for hosts. For example with the CIDR block 172.31.0.0/16, we have the first 16 bits used for network identification (this is the meaning of /16) and the remaining 16 bits for hosts.

Obtaining subnets

We have said above that we have a range of 65536 IPs and now let us say we want to create 4 subnets containing 4096 addresses each, how could we do it?

Well, in order to have 4096 addresses per subnet we have to play a little bit with the power of 2 and then we know that ๐Ÿ’๐ŸŽ๐Ÿ—๐Ÿ”=๐Ÿ๐Ÿ๐Ÿ\bold{4096=2^{12}}. This means that 12 bits should be used for hosts and the remaining 20 bits will be used for network. As we said above an IP is composed of 32 bits and then as 12 bits are used for hots, we have the remaining 20 bits for network prefixes.

Thus, we will have this CIDR block as a result.

172.31.0.0/20

Above is going to be our first subnet so now it is time to calculate the IPs for the subnets, I will show how to do two of them and I hope you will be able to get the others.

First subnet

First, it is important to highlight that we will obtain these special IPs

  • Network address, this is the first address and it is obtained by setting all the available bits for hosts (12 in our case) to zero.
  • Subnet mask, to obtain this the bit used for network (first 20 bits in our case) will be set to 1
  • Broadcast address, this is the last address, which is obtained by setting all the bits for hosts (12 in our case) to 1

This means

CIDR block = 172.31.0.0/20, as we calculated above from the original CIDR block = 172.31.0.0/16 we are allocating 4 bits more for network so we end up with 12 bits for hosts, which means ๐Ÿ’๐ŸŽ๐Ÿ—๐Ÿ”=๐Ÿ๐Ÿ๐Ÿ\bold{4096=2^{12}} addresses.

Network address, let’s apply the procedure described above. Thus let’s set up all the hosts’ bits to zero to have.

172.31.00000000.00000000

For the purpose of explaining I am mixing notations (decimal and binary) here and I am showing the 20 bits used for network highlighted in amber while the bits highlighted in blue are for hosts. Thus, the network address is.

172.31.0.0

Subnet mask, In this case, we should put the first 20 bits to 1 so we have.

11111111.11111111.11110000.00000000

and then by calculating the decimal numbers from above binary numbers we have the subnet mask is.

255.255.240.0

Broadcast address, let’s setup the hosts bits (the last 12) to 1 to get the broadcast address.

172.31.00001111.11111111

Again, I am mixing notations (decimal and binary) just for the sake of explaining. Above we can see that we have set all the bits for hosts’ addresses to 1 in order to get the broadcast address, which in this case is.

172.31.15.255

Usable IPs for hosts, Above we have selected two IPs for network address and broadcast address. Thus, it means we have 4096โˆ’2=40944096 -2=4094 IPs usable for hosts.

First IP in the range is the next one after the network address 172.31.0.0, which means 172.31.0.1

172.31.00000000.00000001

Above we can see that the last bit is equal to 1 to obtain 172.31.0.1

Last IP in the range is the one before the the broadcast address 172.31.15.255, which means 172.31.15.254

172.31.00001111.11111110

Above we can see that the last bit is equal to 0 to obtain 172.31.15.254

Thus, the usable range of IPs for hosts is [172.31.0.1 – 172.31.15.254]

Second subnet

In the previous step we got the subnet with broadcast address (last IP) equal to 172.31.15.255 and then we can use the next one available for the next subnet. Thus, we will proceed in this way to have

CIDR block = 172.31.16.0/20, below we have the explanation

IP broadcast previous subnet172.31.15.255
CIDR block second subnet172.31.16.0/20

Above we are adding 1 to the third octet to get 16.

Network address, let’s set up all the hosts’ bits to zero to have.

172.31.00010000.00000000

Above we can see the last bit of the third octect is 1 to get the 16 needed in the CIDR block 172.31.16.0/20. Thus, the network address is.

172.31.16.0

Subnet mask, In this case, we should put the first 20 bits to 1 so we have.

11111111.11111111.11110000.00000000

and then we have

255.255.240.0

Broadcast address, let’s setup the hosts bits (the last 12) to 1 to get the broadcast address.

172.31.00011111.11111111

In this case we have

172.31.31.255

Usable IPs for hosts, Above we have selected two IPs for network address and broadcast address. Thus, it means we have 4096โˆ’2=40944096 -2=4094 IPs usable for hosts.

First IP in the range is the next one after the network address 172.31.16.0, which means 172.31.16.1

172.31.00010000.00000001

Above we can see that the last bit is equal to 1 to obtain 172.31.16.1

Last IP in the range is the one before the the broadcast address 172.31.31.255, which means 172.31.31.254

172.31.00011111.11111110

Above we can see that the last bit is equal to 0 to obtain 172.31.31.254

Thus, the usable range of IPs for hosts is [172.31.16.1 – 172.31.31.254]

Conclusion

Above it has been shown how to create subnets from a CIDR block, which is summarized in below table.

CIDR BlockSubnet maskNetwork AddressBroadcast addressRange of usable IPs for hosts
172.31.0.0/20255.255.240.0172.31.0.0172.31.15.255172.31.0.1 – 172.31.15.254
172.31.16.0/20255.255.240.0172.31.16.0172.31.31.255172.31.16.1 – 172.31.31.254

For the other two subnets the procedure is the same so I hope this tutorial can be useful for you.

Leave a comment