The IPv4 subnet mask determines which devices are on your local IP subnet. In this video, you’ll learn about the structure of the IPv4 subnet mask and how to calculate the subnet mask based on binary and CIDR-block notations.
In this video, we’re going to go into detail with IPv4 subnet masks. So if you haven’t already watched the video on binary math and classful subnetting, make sure you check the video description and go back and look at those videos first. Most of what we’ll do in this video focuses on some techniques that you’ll learn in those previous two videos.
A subnet mask is a contiguous series of 1s. You’ll notice that all of the 1s are on the left side of the subnet mask, and the 0s are on the right side of the subnet mask. For example, this is a subnet mask. You can see there are contiguous 1s all the way from the left up until this point, and the remainder of the subnet mask is all 0s. You will not have a subnet mask that has interlacing 1s and 0s between it. All the 1s will always be on the left, and all the 0s will always be on the right.
If you were to convert this binary value into decimal values, you’ll find that this subnet mask is also 255.255.255.0 in decimal. And you can also use a shortcut to describe this subnet mask by counting up the number of 1s and putting that right after the slash value. So this subnet mask would also be referred to as a /24.
We refer to this shortcut of using the slash with the number after it as CIDR block notation. That’s C-I-D-R. It stands for Classless Inter-Domain Routing. You might also hear this referred to as CIDR block notation, slash notation, or prefix notation.
The subnet mask is used to delineate the network part of the IP address from the host part of the IP address. All of the 1s designate the network portion. In this case, there are 24 bits of the network portion of the subnet mask, and there are eight bits set to 0. That would be the host portion of the subnet mask.
Let’s do another subnet mask calculation. This time, we’ll take this subnet mask of all 1s in the first two octets and all 0s in the last two octets. If we were to write all of these out as the number of bits that are set to 1 in each of those octets, we have eight in the first octet, eight in the second, zero in the third, and zero in the fourth. If you add all of those up, you can summarize this subnet mask as a /16 because we have 8 plus 8 plus 0 plus 0, which equals 16. If you break down the subnet mask into the portion designated with 1s and the portion with 0s, you can see that there are 16 bits on the network side and 16 bits set to 0 that would be the host side.
Let’s do another CIDR block notation calculation, this time with a subnet mask that doesn’t follow those 8-bit octets. In this example, we have a subnet mask that has all 1s in the first three octets, but the last octet has 11000000. But it’s the same calculation that we’ve used for everything else. We count up the number of 1s, which is 8 plus 8 plus 8 plus 2, which means that the total number of ones would be 26. That means that the CIDR block notation for the subnet mask is a /26. This means that the network portion of the subnet mask is 26 bits long, and the host portion is 6 bits long.
Let’s do one more of these CIDR block notation calculations. In this subnet mask, we have all 1s in the first octet, we have 11110000 in the second octet, and then the third and fourth octet are all 0s. If we count up the number of 1s, we have eight in the first octet and four in the second, which means that this subnet mask is also known as a /12. This means that the network portion of the subnet mask is 12 bits in length, which means all of the remaining bits are the host bits. In this case, that would be 20 bits.
One of the things that we will commonly do when seeing these binary subnet masks is try to convert them back to decimal. And one easy way to do this is to build a chart of all of the possible combinations. For example, all eight bits set to 0 would obviously be a decimal value of 0. If that first bit is set to 1, the decimal value is 128, the first two bits set to 1 are 192, the first three bits set to 1 are 224, and so on.
If you refer back to our video on binary math, then you’ll be able to easily calculate the decimal value from the binary and vice versa. It may help on your exam if you’re able to write out this chart on your whiteboard, either the physical one in a testing center or the digital whiteboard that they give you online, so you have an easy way to reference these values without having to perform manual calculations each time.
So let’s perform another calculation of the subnet mask, but we’re going to use our chart here to perform a quick binary to decimal conversion. Here’s our subnet mask. It’s 11111111.11110000, and then the last two octets are all 0s. Again, we would look at these binary versions of the subnet mask and quickly convert them to decimal.
All 1s is the same thing as a decimal 255, and the conversion of 11110000, if we look at our chart, converts to 240. That means that this subnet mask written in decimal is 255.240.0.0. And if we count the number of 1s in the subnet mask, we can see that there are 12 1s, which means that this subnet mask could also be written as a /12. That means that 12 bits in the subnet mask are the network bits, and the last 20 bits are the host bits.
Let’s do another subnet mask calculation. In this case, we have all 1s in the first two octets, in the third octet, we have three 1s and five 0s, and the last octet is all 0s. If we look at our binary to decimal conversion chart, we can see that the first two octets are 255. The third octet is three 1s and five 0s, and that is 224.
You can see we bring the 224 down. And all 0s is obviously 0, which means that this subnet mask is 255.255.224.0. And if we count the number of 1s, we can see that this is a /19 subnet mask with 19 bits for the network and 13 bits left for the host.
Let’s do this same calculation, but let’s do it in reverse. Let’s start with a /26. One of the first things we can do is to write that out in binary, which means there will be 26 1s. And if we write that out into our four octets, our first three octets are all 1s, the last octet is 11, and all of the 0s are remaining. If we perform the conversion from binary to decimal, then this /26 is also known as a 255.255.255.192 subnet mask. That means that there are 26 bits set aside for the network, and the remaining six bits are the host bits.
And let’s do one more of these in reverse with a /20 subnet mask. We’ll write out all of those 1s in binary form. A /20 means there are 20 1s, so our first two octets are all 1s, the third octet is 11110000, and the last octet is all 0s. If we perform our conversion from binary to decimal, that means that the /20 subnet mask is 255.255.240.0. Our network bits would be 20 bits, and our host bits are the remaining 12 bits. Being able to perform these calculations quickly will help you during your subnetting questions, so you may want to try taking a number of different slash values or binary values and see if you can calculate back what the decimal form and slash values might be for all of these subnet masks.