How to calculate a subnet mask
Calculating a subnet mask is a short, mechanical process once you know the steps: count the hosts you need, find the host bits that cover them, then read off the mask. Here is the method, worked through with a real example.
Step 1: Work out how many hosts you need
Start with the largest number of devices that will ever sit on this subnet at once, then add a little headroom for growth. Every subnet loses two addresses to the network and broadcast address, except /31 and /32, so budget for that automatically. You do not subtract it yourself; the steps below already account for it.
Say a new office floor needs room for 40 devices: laptops, phones, printers, and access points. That host count is what drives every step that follows.
Step 2: Find the smallest power of two that covers it
Usable host counts follow the pattern 2, 6, 14, 30, 62, 126, 254 and upward, each one two short of a power of two (2^h - 2 for h host bits). Find the first number in that list that is at least as large as your host count.
For 40 hosts, 30 is too small and 62 is the first one that fits. 62 usable hosts needs 6 host bits, because 2^6 - 2 = 62.
Step 3: Convert the host bits to a prefix
An IPv4 address has 32 bits total, so the prefix length is 32 minus the host bits. With 6 host bits, the prefix is 32 - 6 = /26.
Prefix length and host bits always trade off: more host bits means a shorter, less specific prefix and a bigger network; fewer host bits means a longer prefix and a smaller one.
Step 4: Turn the prefix into a mask
A /26 means the first 26 bits are 1s and the remaining 6 are 0s: 11111111.11111111.11111111.11000000 in binary. Group those bits into four octets and convert each to decimal to get 255.255.255.192.
You rarely need to do this bit by bit once you know the eight values a masked octet can take: 0, 128, 192, 224, 240, 248, 252, 254, or 255. The subnet mask calculator converts any prefix, or the host count itself, to a mask instantly.
Worked example: 40 hosts
Put it together: 40 hosts needs the next host-count boundary up, which is 62 (6 host bits). 32 - 6 = /26, and a /26 mask is 255.255.255.192. Assigned to 192.0.2.0, that gives the network 192.0.2.0/26, usable hosts 192.0.2.1 through 192.0.2.62, and broadcast 192.0.2.63, room for 62 devices with 22 addresses of headroom over the 40 you started with.
See this exact math worked out for other host counts on the subnet by host count page, or go the other direction, from a prefix to its host count, on the CIDR cheat sheet.
Frequently asked questions
What is the formula for a subnet mask?
There is no single formula so much as a lookup: pick the number of host bits h you need, and the mask has 32 - h leading 1 bits followed by h trailing 0 bits. The host count that fits is 2^h - 2 (2^h for the special cases /31 and /32).
How many host bits do I need for 100 hosts?
2^7 - 2 = 126, the first host-bit count that covers 100 (2^6 - 2 = 62 is not enough). That is 7 host bits, so the prefix is /25 and the mask is 255.255.255.128.
Can I calculate a subnet mask without a calculator?
Yes. Memorise the host-count boundaries 2, 6, 14, 30, 62, 126, 254, find the first one at or above your requirement, and subtract that many host bits from 32 to get the prefix.
Do I need to calculate the mask by hand?
Not for daily work; the subnet mask calculator and the subnet by host count page do it instantly. Knowing the manual method still helps you sanity-check the result and answer it correctly on certification exams that expect the working.