IPv4 Subnet Math: CIDR, Host Counts, and Mental Shortcuts
Subnet notation looks opaque until you see that it is arithmetic on a 32-bit number. A few anchor values and one block-size calculation cover most of what you need day to day.
Why subnets exist at all
An IPv4 address is a 32-bit number written as four 8-bit chunks. 192.168.1.42 is just a friendly name for the integer 3232235818. A subnet is a range of consecutive 32-bit integers that all share the same prefix. A subnet mask, or its modern equivalent CIDR notation, is a way of saying "the first N bits are the network, the remaining 32 minus N bits are the hosts."
Most subnetting comes down to one conversion: N host bits give 2^N addresses, of which 2^N minus 2 are usable hosts on a normal subnet.
Anchor points worth knowing
You do not need a full lookup table. A handful of values lets you work out the rest:
- /32: one address. A single host, used for loopbacks and host routes.
- /31: two addresses, two usable hosts (special-cased per RFC 3021). Used for point-to-point links.
- /30: four addresses, two usable hosts. The classic point-to-point link size.
- /29: eight addresses, six usable hosts. A common size for a few servers or a small VLAN.
- /24: 256 addresses, 254 usable hosts. What the old "class C" size looks like, and common on home and office LANs.
- /16: 65,536 addresses, 65,534 usable hosts. The old "class B" size, such as
192.168.0.0/16. - /8: 16,777,216 addresses. The old "class A" size, such as
10.0.0.0/8.
To go from prefix to host count: subtract the prefix from 32 to get host bits, then compute 2^host_bits - 2. The minus-2 is for the network address (all host bits zero) and the broadcast address (all host bits one). Both are unusable for an actual host.
For a /27, that is 32 minus 27 = 5 host bits, 2^5 minus 2 = 30 usable hosts. For a /28, four host bits, 14 usable hosts. Each step to a shorter prefix doubles the address count, so usable hosts run 6, 14, 30, 62, 126, 254 from /29 to /24: double the previous number and add two.
How to subnet in your head
Take 10.5.20.0/22 and split it into /24s. You could convert to binary, but there is a shorter way:
- A /22 is two bits "smaller" than a /24, so it contains 2^2 = 4 /24 subnets.
- Each /24 is 256 addresses, so the third octet goes up by 1 from one subnet to the next.
- Starting from
10.5.20.0, the four /24s are10.5.20.0/24,10.5.21.0/24,10.5.22.0/24,10.5.23.0/24.
A /22 boundary always falls on a multiple of 4 in the third octet. 10.5.21.0/22 is not a network address, because 21 is not a multiple of 4; it is an address inside 10.5.20.0/22. The /22 networks at 10.5.x.0 start at third octets 0, 4, 8, 12, 16, 20, 24, and so on.
Finding the network for any address
Work out the block size in the octet where the prefix ends, then round the address down to a multiple of it. For 10.0.12.34/27, the prefix ends in the fourth octet and leaves 5 host bits, so the block size is 2^5 = 32. The multiples are 0, 32, 64, and so on, and 34 rounds down to 32. The network is 10.0.12.32, the broadcast is one less than the next block, 10.0.12.63, and hosts run from .33 to .62.
The point-to-point oddity (/31 and /30)
When you connect two routers with a direct link, you only need two addresses (one for each end). Allocating a /24 for that is wasteful. The classical answer is a /30, which gives you four addresses: network, host A, host B, broadcast. Two usable out of four.
RFC 3021 defined /31 for point-to-point links, treating both addresses as usable and removing the broadcast address. That halves the addresses used per link. Most current routing equipment supports it, but check both ends before you rely on it.
The supernetting trick
Subnetting splits a big block into smaller blocks. Supernetting (or "CIDR aggregation") combines smaller blocks into a bigger one, but only when they are aligned. 10.0.0.0/24 and 10.0.1.0/24 can be aggregated into 10.0.0.0/23. But 10.0.1.0/24 and 10.0.2.0/24 cannot, because 10.0.1.0 is not a /23 boundary (the boundaries fall on 0, 2, 4, 6...).
Aggregation keeps routing tables smaller, because one prefix can replace several more-specific ones. The global internet routing table depends on it.
Private vs. public ranges, and why it matters
RFC 1918 reserves three ranges for private use. They are not routed on the public internet:
10.0.0.0/8: about 16.7 million addresses.172.16.0.0/12: about 1 million addresses.192.168.0.0/16: 65,536 addresses. The usual choice on home networks.
If you see a packet on the public internet with a source of 10.5.5.5, something has gone wrong (or someone has misconfigured NAT). The point of these ranges is to let many independent networks reuse the same address space without coordination, because NAT translates them to public addresses at the boundary.
Common mistakes
Confusing prefix length with usable hosts. A /28 has 16 addresses but only 14 usable hosts. Allocating a /28 for "a team of 14 people" leaves zero growth room. Always size up.
Forgetting the gateway uses an address. A /24 has 254 usable hosts, but the default gateway takes one, and network devices or reserved DHCP ranges often take a few more. Plan for fewer than the theoretical count.
Overlapping subnets. 10.0.0.0/22 and 10.0.0.0/24 overlap; the second is inside the first. In a routing table both can exist, and the longest matching prefix wins. On interfaces or in VPC-style address plans, overlaps are usually rejected or cause traffic to go to the wrong place, so check for them before you allocate.
Hard-coding /24 everywhere. A /24 is a convenient default but not always the right size. A subnet for a few printers needs far fewer addresses, and a /29 may be plenty.
Where the subnet calculator helps
When a calculation is more than you want to do in your head, the IPv4 subnet calculator shows the network address, broadcast, first and last usable host, and total addresses on one screen.
To check your understanding: how many /27s fit in a /22? The difference is 5 bits, so 2^5 = 32.