Subnetting Made Simple: CIDR, Subnet Masks and Host Calculations
Learn subnetting step by step: what CIDR notation means, how to split a /24 into smaller subnets, and how to calculate network, broadcast and usable hosts.
Subnetting means dividing one IP network into smaller networks called subnets. It's how cloud VPCs, office networks and data centers organise addresses – and it's a classic interview topic.
Why subnet at all?#
- Organisation – separate networks for web servers, databases, office staff, guests.
- Security – firewalls and routing rules can be applied between subnets.
- Performance – smaller broadcast domains mean less noisy traffic.
- Efficient address use – give each team only as many addresses as it needs.
CIDR notation in 30 seconds#
CIDR (Classless Inter-Domain Routing) writes the mask as a prefix length: 192.168.1.0/24 means the first 24 bits are the network. The remaining 32 − 24 = 8 bits are for hosts.
- Total addresses = 2host bits
- Usable hosts = 2host bits − 2 (one for the network address, one for the broadcast address)
So a /24 has 28 = 256 addresses and 254 usable hosts.
Worked example: split a /24 into four subnets#
- How many subnets? We need 4. 22 = 4, so borrow 2 bits from the host portion.
- New prefix: 24 + 2 = /26. Mask =
255.255.255.192. - Block size: 256 − 192 = 64. Each subnet starts 64 addresses after the previous one.
- List the subnets:
| Subnet | Network | Usable range | Broadcast |
|---|---|---|---|
| 1 | 192.168.1.0/26 | .1 – .62 | .63 |
| 2 | 192.168.1.64/26 | .65 – .126 | .127 |
| 3 | 192.168.1.128/26 | .129 – .190 | .191 |
| 4 | 192.168.1.192/26 | .193 – .254 | .255 |
The "magic number" trick: block size = 256 − the interesting octet of the mask. For /26 the mask's last octet is 192, so subnets go up in steps of 64.
CIDR cheat sheet#
A few you'll meet constantly:
- /32 – a single host (common in firewall rules and routes).
- /30 – 2 usable hosts, classic for point-to-point router links.
- /24 – 254 hosts, the default home or small-office network.
- /16 – 65,534 hosts, a typical cloud VPC size.
Find the subnet of any IP#
Which subnet does 192.168.1.150/26 belong to? Block size is 64. The multiples of 64 are 0, 64, 128, 192. 150 falls between 128 and 191, so it's in 192.168.1.128/26, and its broadcast is .191.
Programmatically, a bitwise AND does the job:
import ipaddress
iface = ipaddress.ip_interface("192.168.1.150/26")
net = iface.network
print(net) # 192.168.1.128/26
print(net.broadcast_address) # 192.168.1.191
print(net.num_addresses - 2) # 62 usable hostsAWS, Azure and GCP reserve extra addresses in every subnet (AWS reserves 5). Always plan a little headroom.
Key takeaways#
- CIDR /n means n network bits; the rest are host bits.
- Usable hosts = 2host bits − 2.
- Borrow bits from the host portion to create 2bits subnets.
- Block size = 256 − mask octet; subnets start at multiples of it.