To calculate a subnet mask from a CIDR prefix, set the first N bits of a 32-bit number to 1 and the rest to 0, then write it as four decimal numbers. A /24 is 255.255.255.0 and a /26 is 255.255.255.192. The number of usable hosts is 2 raised to the power of the host bits, minus 2 for the network and broadcast addresses, so a /24 has 254 usable hosts.
Key takeaways
- A subnet mask has 1 bits for the network part and 0 bits for the host part, in one unbroken block.
- Usable hosts equal 2 to the power of the host bits, minus 2 for the network and broadcast addresses.
- The block-size method finds subnet boundaries fast: block size equals 256 minus the interesting mask octet.
A subnet mask looks like four random numbers, but it is really one simple idea: the first part of an IPv4 address names the network, and the rest names the host. Once you can turn a CIDR prefix such as /24 into a mask, you can find the network address, the broadcast address and how many hosts fit.
What is a subnet mask?
A subnet mask is a 32-bit number that splits an IPv4 address into a network part and a host part. Its 1 bits mark the network part and its 0 bits mark the host part, in one unbroken block. The mask is written as four decimal numbers, one for each group of 8 bits.
Take 192.168.1.0 with the mask 255.255.255.0. The mask in binary is 11111111.11111111.11111111.00000000, so the first 24 bits are the network and the last 8 bits are hosts. That is why the same network is written in CIDR notation as 192.168.1.0/24.
Routers use the mask to decide whether a destination is on the local network or needs to be sent to a gateway. A device finds its own network address by doing a bitwise AND of its IP address and its subnet mask. If you get the mask wrong, traffic goes to the wrong place or nowhere at all.
What is CIDR notation?
CIDR, or Classless Inter-Domain Routing, writes a network as an address followed by a slash and a prefix length, such as 192.168.10.0/24. The prefix length is the number of leading 1 bits in the mask. RFC 4632 defines this notation and replaced the old class-based system.
The prefix tells you how many bits are network bits. A /24 has 24 network bits and 8 host bits. A /16 has 16 network bits and 16 host bits. A /26 has 26 network bits and 6 host bits. The larger the prefix, the smaller the network.
Old address classes, Class A, B and C, no longer decide the mask. A network starting with 192 is not automatically a /24, and a network starting with 10 is not automatically a /8. Modern allocation and routing use explicit prefixes, so always use the prefix you are given.
How do you calculate a subnet mask from CIDR?
Set the first N bits of a 32-bit number to 1 and the rest to 0, then write each group of 8 bits as a decimal number. That is the whole method, and it works for every prefix from /0 to /32.
Start with the bit values in one octet: 128, 64, 32, 16, 8, 4, 2, 1. A full octet of 1 bits is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1, which is 255. An octet with no 1 bits is 0. A partial octet adds only the bits that are set.
Worked examples make this concrete. A /24 is 24 full bits, so three octets are 255 and the last is 0: 255.255.255.0. A /26 is 24 full bits plus 2 more, and 128 + 64 is 192, so the mask is 255.255.255.192. A /20 is 16 full bits plus 4, and 128 + 64 + 32 + 16 is 240, so the mask is 255.255.240.0.
For a quick check, remember the partial-octet values: 1 bit is 128, 2 bits are 192, 3 bits are 224, 4 bits are 240, 5 bits are 248, 6 bits are 252 and 7 bits are 254. If you know those seven numbers, you can convert any prefix in seconds.
What are the common subnet masks?
These are the prefixes you will meet most often in home and office networks. Learn the /24, /25, /26, /27 and /30 rows first, because they cover most small-network work.
| Prefix | Subnet mask | Total addresses | Usable hosts |
|---|---|---|---|
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /20 | 255.255.240.0 | 4,096 | 4,094 |
| /22 | 255.255.252.0 | 1,024 | 1,022 |
| /24 | 255.255.255.0 | 256 | 254 |
| /25 | 255.255.255.128 | 128 | 126 |
| /26 | 255.255.255.192 | 64 | 62 |
| /27 | 255.255.255.224 | 32 | 30 |
| /28 | 255.255.255.240 | 16 | 14 |
| /29 | 255.255.255.248 | 8 | 6 |
| /30 | 255.255.255.252 | 4 | 2 |
| /31 | 255.255.255.254 | 2 | 2 |
| /32 | 255.255.255.255 | 1 | 1 |
Notice the pattern: every extra prefix bit halves the block. A /24 has 256 addresses, a /25 has 128 and a /26 has 64. The /31 and /32 rows are special cases, explained below.
How many usable hosts are in a subnet?
If a prefix leaves h host bits, the block has 2 to the power of h total addresses. On a conventional IPv4 broadcast network, the usable host count is 2 to the power of h, minus 2, because one address is the network address and one is the broadcast address.
For a /24, h is 8, so 2 to the power of 8 is 256, minus 2 is 254 usable hosts. For a /26, h is 6, so 64 minus 2 is 62 usable hosts. For a /30, h is 2, so 4 minus 2 is 2 usable hosts, which is why /30 is the standard size for a point-to-point link.
The minus 2 rule has exceptions. A /31 link under RFC 3021 has 2 addresses and both can be used, because there is no network or broadcast address on a point-to-point link. A /32 is a single host address, used for a loopback or a single host route. Do not apply minus 2 blindly to those two cases.
How do you find the network and broadcast addresses?
The fastest manual method is the block-size method. Find the mask octet that is neither 255 nor 0, then calculate block size as 256 minus that octet. Subnet boundaries fall on multiples of the block size in that octet.
Take 192.168.10.130 with mask 255.255.255.192. The interesting octet is 192, so the block size is 256 minus 192, which is 64. Subnet boundaries in the last octet are 0, 64, 128 and 192. The address 130 falls in the 128 block, so the network address is 192.168.10.128.
The broadcast address is the last address in the block: network address plus block size, minus 1. Here that is 128 plus 64, minus 1, which is 191, so the broadcast is 192.168.10.191. The usable host range is 192.168.10.129 to 192.168.10.190.
The same result comes from a binary AND: IP address AND subnet mask equals the network address. For 192.168.10.130 AND 255.255.255.192, the last octet is 130 AND 192, which is 128. Use the block-size method for speed and the AND method when you want to check your work.
How do you choose a subnet size for a number of hosts?
Start from the host requirement and work backwards. Find the smallest number of host bits where 2 to the power of h, minus 2, is at least the number of hosts you need. Then subtract h from 32 to get the prefix, and convert the prefix to a mask.
Suppose you need at least 50 hosts per subnet. Try 5 host bits: 32 minus 2 is 30, which is too few. Try 6 host bits: 64 minus 2 is 62, which is enough. So you need 6 host bits, the prefix is 32 minus 6, which is /26, and the mask is 255.255.255.192.
Suppose you need 8 subnets from a /24. You need 3 borrowed bits, because 2 to the power of 3 is 8. The new prefix is 24 plus 3, which is /27, and the mask is 255.255.255.224. Each /27 subnet then has 30 usable hosts.
Always leave room to grow. A subnet that is exactly full on day one will need renumbering when the team grows. Choose the next size up if growth is likely, and document why you chose that size.
What is VLSM and when should you use it?
Variable Length Subnet Masking, or VLSM, means using different prefix lengths inside one larger network, so each subnet is only as large as it needs to be. It avoids wasting addresses on small subnets such as point-to-point links.
For example, a /24 can be split into one /26 for 50 hosts, one /27 for 25 hosts, and several /30 links for routers, with space left over. With fixed-length subnetting, every subnet would be the same size, so the small links would waste most of their addresses.
VLSM is normal in modern networks because CIDR and modern routing protocols support it. The rule is simple: allocate the largest subnets first, then the smaller ones, and keep a written plan so nobody later allocates an overlapping block.
What are common subnetting mistakes?
Most subnetting mistakes are small arithmetic errors or a wrong assumption about the prefix. Check your work with the block-size method before you apply a change to a live network.
- Forgetting minus 2. Counting 256 hosts in a /24 instead of 254 usable hosts.
- Using a class as a mask. Assuming 192.168.0.0 must be a /24, or 10.0.0.0 must be a /8.
- Non-contiguous masks. A mask such as 255.0.255.0 is invalid because the 1 bits must come first in one block.
- Overlapping subnets. Allocating 192.168.1.0/25 and 192.168.1.64/26, which overlap.
- Wrong broadcast. Using the network address or the broadcast address as a host address.
- Ignoring /31 and /32. Applying minus 2 to a point-to-point /31 link, where both addresses are usable.
Subnetting is also a security control. Smaller subnets limit broadcast traffic and make it easier to segment traffic between departments, guests and servers. Our guide to how to secure your Wi-Fi covers segmentation at home, and the Portnox guide to subnet masks gives another worked example of the 50-host calculation.
For a second worked example, see the MEF Mobile subnetting guide.
You can review network segmentation alongside subnetting in our small business cybersecurity checklist.
How does subnetting work in IPv6?
IPv6 uses the same CIDR idea, but with 128 bits instead of 32. A typical home or office assignment is a /64, which leaves 64 bits for hosts, an enormous number. You do not calculate host counts in IPv6 the way you do in IPv4.
The common IPv6 task is splitting a larger prefix into /64 subnets. If you receive a /56, you have 8 bits to allocate, which gives 256 possible /64 subnets. If you receive a /48, you have 16 bits, which gives 65,536 possible /64 subnets.
Do not try to apply IPv4 habits such as conserving addresses to IPv6. The address space is large enough that the goal is a clear, documented plan, not squeezing the last address out of a block.
The bottom line
Calculating a subnet mask is a three-step skill: turn the prefix into 1 bits and 0 bits, convert each octet to decimal, then use the block size to find the network, broadcast and host range. Practice it with /24, /26 and /27 until the partial-octet values feel automatic.
When you plan a real network, write the plan down, leave room to grow, and check overlaps before you apply anything. You can confirm the public address your network presents with our free what is my IP tool.
Frequently asked questions
What is a subnet mask?
A subnet mask is a 32-bit number that divides an IPv4 address into a network part and a host part. The 1 bits mark the network part and the 0 bits mark the host part. For example, 255.255.255.0 means the first 24 bits are the network and the last 8 bits are hosts.
How do I calculate a subnet mask from CIDR?
Set the first N bits to 1 and the rest to 0, then convert each group of 8 bits to decimal. A /24 gives 255.255.255.0 and a /26 gives 255.255.255.192, because two bits set at the left of an 8-bit number are 128 plus 64.
How many usable hosts are in a /24?
A /24 has 254 usable hosts. It has 8 host bits, so 2 to the power of 8 is 256 total addresses, minus the network address and the broadcast address.
What is the difference between a subnet mask and a wildcard mask?
A wildcard mask is the opposite of a subnet mask. Subtract each octet of the subnet mask from 255. For 255.255.255.0 the wildcard is 0.0.0.255, and for 255.255.255.192 it is 0.0.0.63. Wildcard masks are used in access lists.
Do I still need to know address classes?
No, not for modern networks. CIDR replaced class-based assignment, as described in RFC 4632. A network starting with 192 is not automatically a /24. Use the prefix you are given or that you choose.
What is a /31 subnet used for?
A /31 is used for point-to-point links between two routers. It has only 2 addresses and, under RFC 3021, both can be used as host addresses, so the usual minus 2 rule does not apply there.
How do I choose a subnet size for a number of hosts?
Find the smallest power of 2 that covers the hosts you need plus 2 for network and broadcast. For 50 hosts you need 6 host bits, because 2 to the power of 6 minus 2 is 62, which gives a /26 mask of 255.255.255.192.
Sources
The engineers and analysts who build Cysoro's domain scanner, email-authentication checks and free security tools. We test what we write about every day — SPF, DKIM, DMARC, certificates, look-alike domains and phishing — and check each guide against the relevant standards, government guidance and vendor documentation.
Checked against RFC 4632 for CIDR notation and RFC 3021 for /31 point-to-point links, plus vendor and training guides on subnet calculation.
How we write and fact-check →