Subnet Masks and How to Calculate Them
Convert between masks and prefix lengths, then find any network, broadcast and host range using the block size method.
Key takeaways
- A subnet mask is 32 bits: an unbroken run of ones for the network part followed by zeros for the host part.
- Only nine values can appear in a mask octet: 0, 128, 192, 224, 240, 248, 252, 254 and 255.
- The block size equals 256 minus the mask value in the octet where the boundary falls.
- Usable hosts in an ordinary subnet equal 2 to the number of host bits, minus two.
To calculate with a subnet mask, find the one octet where the mask is not 255 or 0, subtract that value from 256 to get the block size, and count in steps of that size until you reach your address. That gives you the network address, and the broadcast address sits just before the next step.
The rest of this article shows why the shortcut works, how to build a mask from a prefix length or a host count, and how to avoid the errors that usually creep in.
What a mask is for
Every IPv4 address is paired with a mask that says which part identifies the network. A device uses it constantly. Before sending a packet, it applies its own mask to the destination and compares the result with its own network. A match means the destination is local. Anything else goes to the default gateway.
The mask is never sent inside ordinary packets. It is local configuration, set by hand or delivered by DHCP, so two devices on the same wire can hold different opinions about where the network ends. Many confusing faults come from exactly that.
The nine legal octet values
Because the ones must be continuous, each octet of a mask can only take values made by filling bits from the left.
| Bits set | Binary | Decimal | Block size |
|---|---|---|---|
| 0 | 00000000 | 0 | 256 |
| 1 | 10000000 | 128 | 128 |
| 2 | 11000000 | 192 | 64 |
| 3 | 11100000 | 224 | 32 |
| 4 | 11110000 | 240 | 16 |
| 5 | 11111000 | 248 | 8 |
| 6 | 11111100 | 252 | 4 |
| 7 | 11111110 | 254 | 2 |
| 8 | 11111111 | 255 | 1 |
Memorize this table and you can do nearly any IPv4 subnet question without writing binary. A mask such as 255.255.255.200 is invalid, since 200 is 11001000 and the ones are interrupted.
Converting between prefix and mask
To go from prefix length to mask, deal the ones into octets, eight at a time. For /19: the first two octets take 16 bits and become 255.255. Three bits remain, which makes 224 in the third octet. The fourth is zero. The mask is 255.255.224.0.
To go from mask to prefix length, count 8 for each 255, then add the bit count for the partial octet. 255.255.255.248 is 8 + 8 + 8 + 5, so /29.
The slash form and the dotted form mean exactly the same thing. CIDR notation covers the slash style, which is what you will see in most modern documentation. Dotted masks still appear in device settings screens and many command line tools.
The block size method
Here is the full procedure, applied to 192.168.10.77 with mask 255.255.255.224.
- Find the interesting octet. The fourth, with value 224.
- Compute the block size. 256 minus 224 is 32.
- List the boundaries near the address. 0, 32, 64, 96.
- Network address: the highest boundary not above 77, which is 64. So
192.168.10.64. - Broadcast address: one less than the next boundary, 96. So
192.168.10.95. - Usable hosts:
.65through.94, which is 30 addresses.
Octets to the left of the interesting one are copied unchanged. Octets to the right become 0 in the network address and 255 in the broadcast address. That rule matters in the next two examples.
Why the shortcut works
The block size is more than a trick. In the interesting octet, the mask's ones cover the high bit positions and its zeros cover the low ones. The low positions can take every combination, so a block always spans as many values as those zero bits allow. With three zeros, that is 8 values. With five zeros, it is 32.
Subtracting the mask value from 256 gives the same number, because an octet has 256 possible values and the mask value is what remains after removing one block's worth from the top. For 224, the missing piece is 32.
Blocks also have to start on multiples of their own size. A block's starting point has all of the host bits at zero, and only multiples of 32 have their five lowest bits at zero. Once you see that, you stop memorizing boundaries and simply count.
A second shortcut helps with large octet values. Instead of listing every multiple, divide. For an address octet of 200 and a block size of 32, 200 divided by 32 is 6 with a remainder. Six times 32 is 192, so the block starts at 192 and the next one starts at 224.
Worked example: the third octet
Take 172.16.45.200 with mask 255.255.240.0.
The interesting octet is the third, with value 240, so the block size is 16. Boundaries run 0, 16, 32, 48. The address has 45 in that position, so the block starts at 32.
- Network:
172.16.32.0 - Broadcast:
172.16.47.255 - First host:
172.16.32.1 - Last host:
172.16.47.254 - Prefix:
/20, with 12 host bits, so 4,094 usable hosts
Everything between the network and broadcast addresses is usable, including values like 172.16.40.0 and 172.16.40.255. They look special and are not.
Worked example: the second octet
Take 10.5.200.9 with mask 255.248.0.0.
The interesting octet is the second, with value 248, so the block size is 8. The second octet is 5, which falls in the block starting at 0.
- Network:
10.0.0.0 - Broadcast:
10.7.255.255 - Prefix:
/13
The next block would begin at 10.8.0.0.
Choosing a mask from a host count
Planning goes the other way. Add 2 to the number of devices, then find the smallest power of two that is at least that large.
| Devices needed | Plus 2 | Block size | Prefix | Mask |
|---|---|---|---|---|
| 2 | 4 | 4 | /30 | 255.255.255.252 |
| 10 | 12 | 16 | /28 | 255.255.255.240 |
| 25 | 27 | 32 | /27 | 255.255.255.224 |
| 60 | 62 | 64 | /26 | 255.255.255.192 |
| 100 | 102 | 128 | /25 | 255.255.255.128 |
| 200 | 202 | 256 | /24 | 255.255.255.0 |
| 500 | 502 | 512 | /23 | 255.255.254.0 |
Watch the edges. Sixty-three devices need 65 addresses, which no longer fits in 64, so you move up to a /25. In practice, choose a size with spare room well before you reach the limit.
The binary AND, if you want to see it
The shortcut is a faster version of what hardware does. A logical AND gives 1 only when both bits are 1.
address 45 = 00101101
mask 240 = 11110000
AND = 00100000 = 32 (network)
broadcast: set the host bits to 1
00101111 = 47 The last four bits of that octet, plus all eight of the next, are host bits. Zeroing them gives the network, and setting them all gives the broadcast.
Are two addresses on the same subnet?
This is the question you will ask most often while troubleshooting. Compute the network address for both and compare.
Say a printer is 192.168.8.140 and a laptop is 192.168.8.115, both with mask 255.255.255.128. The block size is 128, so the boundaries are 0 and 128. The printer is in the block starting at 128. The laptop is in the block starting at 0. They are on different subnets and need a router, even though their addresses look close.
With mask 255.255.254.0, the comparison happens in the third octet with a block size of 2. Addresses 192.168.8.115 and 192.168.9.140 share the block that starts at 8, so they are neighbors although their third octets differ.
If the answer is "same subnet" and they still cannot talk, look elsewhere: a device firewall, client isolation on a wireless network, a VLAN assignment or a mismatched mask on one of the two. If the answer is "different subnets", check that each has the right gateway and that the router permits the traffic.
Masks in routes and firewall rules
The same 32-bit pattern appears outside interface settings. A route such as 10.20.0.0 255.255.0.0 means "any destination whose first two octets are 10.20". A firewall rule with mask 255.255.255.255 matches exactly one address, and mask 0.0.0.0 matches all of them.
Some router access lists use the inverse form, where zeros mean "must match". Those are wildcard masks, and confusing the two is a classic way to write a rule that matches nothing you intended. Read the syntax guide for the device before typing either.
Checking a mask on your own device
You can read the mask your computer is using:
- Windows:
ipconfigshows "Subnet Mask" in dotted form. - macOS:
ifconfigoften prints it in hex, such asnetmask 0xffffff00. Read each pair of hex digits as an octet:ff.ff.ff.00is255.255.255.0. - Linux:
ip addrshows the prefix length directly, such as192.168.1.20/24.
Reading your network setup with ipconfig, ifconfig and ip explains the rest of that output. For larger jobs, the subnet host generator can list the addresses in a block.
Changing a mask on a live network
Calculating a new mask is the easy part. Applying it needs a little order, because devices learn the change at different times.
Suppose you widen 192.168.8.0/24 to 192.168.8.0/23. First confirm that 192.168.9.0/24 is unused anywhere, including behind VPNs. Then update the router interface and the DHCP scope. Devices on DHCP pick up the new mask when they renew their leases, so shortening the lease time beforehand speeds things up. Finally, visit everything configured by hand, such as printers, servers and switches.
While old and new masks coexist, a device with the old /24 mask treats 192.168.9.x neighbors as remote and sends that traffic to the router. Many routers will pass it back, so the fault can be subtle: slow or one-sided connections instead of a clean failure. Finish the changeover instead of leaving stragglers.
Notice also that the old broadcast address, 192.168.8.255, becomes an ordinary host address in the wider subnet, and the new broadcast is 192.168.9.255.
Common errors
- Using the wrong octet. Apply the block size only in the octet where the mask is partial.
- Counting from one. Boundaries start at 0: with block size 32, they are 0, 32, 64, never 1, 33, 65.
- Forgetting the minus two. A
/28holds 16 addresses but 14 hosts. - Forgetting the router. The gateway interface takes one of those 14.
- Trusting habit. Not every network starting with
192.168is a/24, and not every10.xnetwork is a/8.
Practice
-
192.168.4.130/26: find the network and broadcast. -
10.20.99.7/22: find the network and broadcast. - Which mask holds 40 devices with the least waste?
Answers: first, network 192.168.4.128, broadcast 192.168.4.191. Second, block size 4 in the third octet, so network 10.20.96.0 and broadcast 10.20.99.255. Third, /26, or 255.255.255.192, since 42 addresses will not fit in 32.
When you are comfortable with these, network, broadcast and host addresses covers the special cases, and the cheat sheet puts every prefix in one place.