CIDR and Subnet Basics for Beginners
Subnetting looks like arcane arithmetic until you notice that it is just binary with a line drawn through it. Once you see an IPv4 address as 32 bits and a prefix as "how many of them name the network", the masks, broadcast addresses and host counts all fall out of a few simple rules. This guide builds that picture and works one complete example you can check by hand.
An IPv4 address is 32 bits
The familiar dotted quad, such as 192.168.10.37, is four 8-bit numbers (octets) written in decimal and separated by dots. Underneath it is a single 32-bit value:
192 .168 .10 .37
11000000 .10101000 .00001010 .00100101
If binary conversion is unfamiliar, the Number Base Converter will do it for you. The key point: an address is split into a network part on the left and a host part on the right. All machines in the same subnet share the network part.
Prefix length and netmask
CIDR (Classless Inter-Domain Routing) notation writes the split as a slash and a number: 192.168.10.37/26 means the first 26 bits are the network and the remaining 6 are the host. The same information can be written as a netmask: 26 ones followed by 6 zeros, which is 11111111.11111111.11111111.11000000 or 255.255.255.192. The two forms are interchangeable. Before CIDR, the split was fixed by class (A, B, C); today it is arbitrary, which is why the old "class" labels are mostly historical.
The rules
- Network address: the address with all host bits set to 0 (address AND mask).
- Broadcast address: all host bits set to 1, the last address in the block.
- Total addresses: 2(32 - prefix).
- Usable hosts: 2(32 - prefix) - 2, since the network and broadcast addresses are not assigned to machines.
Worked example: 192.168.10.37/26
Step 1: a /26 leaves 32 - 26 = 6 host bits, so the block holds 26 = 64 addresses, and 62 are usable.
Step 2: the mask is 255.255.255.192. The first three octets are fully network, so only the last octet needs work. A block size of 64 means the subnets in this /24 start at 0, 64, 128 and 192.
Step 3: find which block contains 37. It is between 0 and 63, so the network address is 192.168.10.0.
Step 4: the broadcast is the last address in that block: 192.168.10.63.
Step 5: usable hosts run from 192.168.10.1 to 192.168.10.62.
Network 192.168.10.0/26
Mask 255.255.255.192
Broadcast 192.168.10.63
Hosts 192.168.10.1 - 192.168.10.62 (62 usable of 64)
These figures were confirmed with Python's ipaddress module, and the IP Subnet Calculator gives the same answer: type 192.168.10.37/26 and press Calculate. It shows the network address, broadcast address, subnet mask, wildcard mask, first and last usable host, total addresses, usable hosts and the IP class. It handles IPv4 only. The wildcard mask is simply the inverse of the netmask, here 0.0.0.63, and is used in some router access lists.
Common prefix sizes
| Prefix | Netmask | Total addresses | Usable hosts |
|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 |
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /24 | 255.255.255.0 | 256 | 254 |
| /26 | 255.255.255.192 | 64 | 62 |
| /28 | 255.255.255.240 | 16 | 14 |
| /30 | 255.255.255.252 | 4 | 2 |
| /31 | 255.255.255.254 | 2 | 2 (point-to-point, RFC 3021) |
| /32 | 255.255.255.255 | 1 | a single host |
Two exceptions to the "minus two" rule: a /31 is used on point-to-point links with no network or broadcast address, and a /32 identifies one host, as in a firewall rule or a route to a single IP.
Special and private ranges
RFC 1918 reserves three blocks for private networks that are not routed on the public internet:
10.0.0.0/8: about 16.7 million addresses.172.16.0.0/12: 172.16.0.0 to 172.31.255.255.192.168.0.0/16: the usual home-router range.
Other ranges worth recognising: 100.64.0.0/10 is shared address space for carrier-grade NAT (CGNAT), which ISPs use between you and their NAT, and is a frequent surprise when a VPN overlaps it. 127.0.0.0/8 is loopback (127.0.0.1 is "this machine"), and 169.254.0.0/16 is link-local, what a device assigns itself when DHCP fails. If you see 169.254.x.x on a machine, name resolution and DHCP are the first places to look; the DNS Lookup tool can check what a hostname resolves to.
Splitting and summarising
Every extra prefix bit halves a block. Splitting 192.168.10.0/24 by two bits (/24 to /26) gives four subnets of 64 addresses:
192.168.10.0/26 hosts .1 - .62
192.168.10.64/26 hosts .65 - .126
192.168.10.128/26 hosts .129 - .190
192.168.10.192/26 hosts .193 - .254
The reverse is summarising (supernetting): merging adjacent blocks into one shorter prefix, which keeps routing tables small. Four consecutive /24s starting on a multiple of four, like 192.168.0.0/24 to 192.168.3.0/24, collapse into 192.168.0.0/22. The blocks must be contiguous and aligned: a /22 has to start on an address divisible by 1024, so 192.168.1.0/22 is not a valid network.
Practical sizing in cloud networks
When you design a VPC, leave room to grow, because changing a subnet's size later is painful. Choose a roomy primary block such as a /16 and carve it into /24 or larger subnets per tier and availability zone. Avoid overlapping the ranges of networks you may later connect through peering or a VPN. Also remember that clouds reserve addresses of their own: in AWS, five addresses in every subnet are reserved (the network address, the VPC router, the DNS server, one for future use and the broadcast), so a /28 subnet, AWS's smallest, offers 11 usable addresses rather than 14.
A short IPv6 note: addresses are 128 bits, and the conventional subnet is a /64, leaving 64 bits for hosts. You do not count hosts to save space; the arithmetic is the same idea at a much bigger scale. This guide's calculator does not cover IPv6.
Subnet maths shows up at the edges of other topics too. A firewall rule with /32 allows exactly one host, and when a request is blocked, the status code it returns is explained in HTTP status codes explained. Practise with the IP Subnet Calculator, predict the network and broadcast by hand first, and you will quickly be reading prefixes at a glance.