Subnetting quick & easy
It’s very useful to be able to quickly perform some subnetting when given an IP address with its CIDR – it allows you to determine how many addresses there are in the corresponding network, if the IP is a broadcast IP, if another IP belongs to the same network, etc. While you might remember some “common” CIDRs like /8, /16, /24, there is also a quick and easy way how to determine the subnet for less well-known CIDRs.
There are many videos on this, but I find Sebastian Philippi’s technique the most straightforward one. As the video is only available in German and as it also omits some details which I think are helpful for a better understanding, I wanted to share this tip with you.
Table of Contents
Composition of an IP address
As you will probably know, an IPv4 Address is composed of 4 numbers separated by a dot, with each number between 0 and 255, e.g. 192.168.178.2 or 127.0.0.1.
Technically spoken, each IP address is a 32-bit binary number.
Why 32 bits? Since each of the 4 numbers can be anything between 0 and 256, you will need up to 8 bits to represent them. 4 times an 8-bit number equals 32 bits. For example:
192.168.178.2 in binary representation would be:
192 = 1*128 + 1*64 + 0*32 + 0*16 + 0*8 + 0*4 + 0*2 + 0*1 168 = 1*128 + 0*64 + 1*32 + 0*16 + 1*8 + 0*4 + 0*2 + 0*1 178 = 1*128 + 0*64 + 1*32 + 1*16 + 0*8 + 0*4 + 1*2 + 0*1 2 = 0*128 + 0*64 + 0*32 + 0*16 + 0*8 + 0*4 + 1*2 + 0*1
If we format this like an IP address, 192.168.178.2 becomes:
Tip: You can easily convert this using some IP-to-Binary online converter, but in practice you will not need to do this. I just explained it here to build a better understanding of our following steps.
Each IP address belongs to a network, which is indicated by a subnet mask. This bitmask is then applied with a bitwise AND to the IP address to determine the subnet of that IP. This process is called subnetting. When you look into your computer settings, you will see a subnet mask written in decimal form, just as an IP address, e.g.
However, in networking scenarios you will often see this represented as a CIDR notation instead, which stands for classless inter domain routing.
No matter if a subnet mask or a CIDR notation is used, in both cases we can see which parts of an address are “fixed” and which can be used by systems in that network. These two parts are known as the network prefix (the part which is fixed), and the host identifier (the variable part).
From a subnet you can simply see which parts are variable. The subnet above – 255.255.255.0 – tells us that the first three of the four octets are fixed (network part) and that the last octet can be used by hosts in the network (0-256).
When we have a CIDR, this is a bit more difficult to see, but as I said in the introduction, there is a quick and easy way how to retrieve the same information. Here is how:
Determining network prefix and host identifier
Remember that each “block” of an IPv4 address is 8 bits long. If we “count” the bits with increasing numbers from left to right, we get:
We can see: The first, second, third… eighth bit belong to the first octet, the 9th, 10th… 16th bit to the second, etc. With this knowledge we can see to which octet a given CIDR belongs.
Let’s check with some examples:
For 192.168.178.2/7 we can see:
7 is in 1-8, so this will be subject to subnetting in the first octet.
For 10.255.13.0/15 we can see:
15 is in 9-16, so this will be subject to subnetting in the second octet.
For 127.15.19.1/20 we can see:
20 is in 17-24, so this will be subject to subnetting in the third octet
For 22.214.171.124/26 we can see:
26 is in 25-32, so this will be subject to subnetting in the fourth octet
Determining the network size
Let’s continue with the third example, 127.15.19.1/20. Since we know that 20 is between 17-24, we also know that our network part and variable part are somewhere in the third octet.
We further know that the third octet starts with the 17th bit (because the bits 17-24 are in the third octet, see above), so if we count from that 17th bit all the way to the “CIDR bit”… 17, 18, 19, 20… it means that the first 4 of the 8 bits in that third octet are fixed for the network (because the CIDR indicates the fixed network bits).
What remains are then 8 bits minus 4 bits= 4 free bits for our host part. 2^4=16. This is the size of our network: We know that our networks will “jump” in steps of 16 in the third octet:
Determining the network address
We now know that with the /20 CIDR we have steps of 16 in the third octet. Our exemplary IP address is 127.15.19.1/20, so there is a 19 in the third octet. Above we saw that one network in a /20 CIDR will be 127.15.16.0/20 and the next 127.15.32.0/20, so the .19 lies in between these two. This means, for 127.15.19.1/20, the corresponding network address is 127.15.16.0/20.
Determining the broadcast address
We figured that 127.15.19.1/20 is in the network 127.15.16.0/20 and that the next network will be 127.15.32.0/20. Since the broadcast address is defined as the “last” address in a subnet, we simply can “deduct one” from that next network (127.15.32.0/20) to receive the broadcast address of our network:
127.15.32.0/20 “-1” = 127.15.31.255
So, we see that 127.15.31.255 is the broadcast address for all IPs in the network 127.15.16.0/20 – the network to which our exemplary IP 127.15.19.1/20 belongs.
Determining the number of available addresses
To quickly calculate how many addresses are available in a network of that size, we can simply deduct the /20 from the 32 available bits in an IPv4 address (alternatively, from 128 bits for an IPv6 address) and take this as exponent to the basis of 2. In our example:
32 bits (in an IPv4 address) – 20 (from our exemplary IP) = 12 bits
2^12 = 4096
In our network 127.15.16.0/20 we therefore have 4096 available IP addresses.
Determining the last available host address
As a final step, we can find out the last IP address in that network that is available to be assigned to a system: It is simply one address “below” the broadcast address. This means:
127.15.31.255 is the broadcast address, therefore, “deducting one” (255 – 1 = 254), leads us to the last available host address: 127.15.31.254.
Some more examples
Just to make sure that you really understood what we were doing, here are some further examples:
For the IP 126.96.36.199/21 we can see that:
- /21 is in 17-24, therefore we will be working in the 3rd octet.
- Fixed bits in the third octet are 17, 18, 19, 20, 21, i.e. 5 fixed bits. As free bits available for our hosts we have 8-5=3 bits. We take these 3 bits as exponent for 2: 2^3=8, i.e. our networks will be separated by steps of 8 in the third octet:
- Our exemplary address lies in the network 188.8.131.52/21, so for our broadcast address we “deduct 1” and get: 184.108.40.206.
- For the number of available addresses we get 32-21=11. Taken as exponent to the base of two we calculate: Our exemplary IP is one of 2^11=2048 available addresses in that network.
- The last available address in that network is “broadcast address minus 1”, therefore: 220.127.116.11 “-1” = 18.104.22.168.
Ok, one last example.
For the IP 192.168.170.14/28 we can see that:
- /28 is in 25-32, therefore we will be working in the 4th octet.
- Fixed bits in the fourth octet are 25, 26, 27, 28, i.e. 4 fixed bits. As free bits available for our hosts we have 8-4=4 bits. We take these 4 bits as exponent for 2: 2^4=16, i.e. our networks will be separated by steps of 16 in the fourth octet:
- Our exemplary address lies in the network 192.168.170.0/28, so for our broadcast address we “deduct 1” and get: 192.168.169.255.
- For the number of available addresses we get 32-28=4. Taken as exponent to the base of two we calculate: Our exemplary IP is one of 2^4=16 available addresses in that network.
- The last available address in that network is “broadcast address minus 1”, therefore: 192.168.169.255 “-1” = 192.168.169.254.