When setting up various services you will often be required to provide a subnet mask for the ip address you are configuring the service for. Sometimes you need to input the mask in the form xxx.xxx.xxx.xxx/xxx.xxx.xxx.xxx, sometimes in terms of a mask width, or bits, in the format xxx.xxx.xxx.xxx/ww, where ‘ww’ is the mask width (also called a CIDR mask). This conversion is something I always have to refresh my memory on and am often frustrated because I always forget the very simple relationship between these two ways to express a netmask value.
If you already understand the general concepts around tcp/ip networking, and binary dec conversion this is not such a huge deal, but if you dont do the conversion enough it is often easy to forget exactly how the two netmask formats relate to each other.
Without going too deeply into the details of how the tcp/ip stack works, I wanted to point out the missing piece that I always forget when doing this particular conversion.
Remember, each octet, or segment, in an IP address can be a value from 0 – 255. This is because in binary, 255 is 11111111, the maximum value of an 8 bit byte (hence the term ‘octet’).
Using the popular 255.255.255.0 netmask as an example, how would we get the mask width value (our ‘ww’ from above)?
First, we take the mask, 255.255.255.0, and convert each octect’s decimal value to binary. Easily done on any calculator by entering the decimal values while in ‘Dec’ mode and then hitting ‘Bin’. And you thought those had no purpose ;). In the end you should get this result:
255.255.255.0 = 11111111.11111111.11111111.00000000
(I’ve filled out the zeros in the last octet to make it apparent we are talking about an eight bit byte, a calculator will just show 0)
Then, we add up all the 1’s in our binary version of the mask:
sum(11111111111111111111111100000000) = 24
So, our width = 24. If we were setting up a service with the IP of 192.168.1.50, with a subnet of 255.255.255.0, we could express that as 192.168.1.50/255.255.255.0 OR 192.168.1.50/24.
Our netmask is 255.255.255.248, the IP address we want to use is 188.8.131.52. Our configuration needs the mask width value: 184.108.40.206/ww. So, we again, convert each octet in our mask to binary from decimal:
255.255.255.248 = 11111111.11111111.11111111.11111000
then, add up all our 1’s:
sum(11111111111111111111111111111000) = 29
So, our value is: 220.127.116.11/29
To sum up: the width value is the sum of all the bits in the binary values of all the octects of the mask.
Remember, expressing a netmask with the format xxx.xxx.xxx.xxx/ww is just a form of shorthand that relates to the binary value of the netmask.
A quick note, you’ll never see a netmask with a binary value that contains a zero followed by a one. This has to do with how the tcp/ip stack works and is outside the scope of this short post, but just know if your conversion is not working out that way, someone probably gave you an invalid netmask value.
Great list of links and resources about tcp/ip and networking: Uri’s TCP/IP Resources List
A calculator page for all these related tasks, for once you understand and get lazy again: Network Calculators