How it is calculated
An IPv4 address is a 32-bit number. The prefix says how many of those bits, counted from the left, name the network; the remaining bits number the hosts inside it. The subnet mask is the same thing written as an address: ones for the network bits, zeros for the host bits.
addresses = 2^(32 − prefix) usable hosts = addresses − 2 network = address AND mask broadcast = network OR (NOT mask) wildcard = NOT mask
Two addresses of every block are taken: the first names the network and the last is its broadcast address. A /31 is the exception used for links between two routers, where both addresses are hosts, and a /32 is one single address, as a cloud provider assigns a floating or failover IP.
Example
192.168.10.37/26: the mask is 255.255.255.192, so the last octet keeps its top two bits. 37 AND 192 = 0, which gives the network 192.168.10.0, the broadcast 192.168.10.63 and 62 usable hosts from .1 to .62.
Prefixes from /16 to /32
| Prefix | Mask | Addresses | Usable |
|---|---|---|---|
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /17 | 255.255.128.0 | 32,768 | 32,766 |
| /18 | 255.255.192.0 | 16,384 | 16,382 |
| /19 | 255.255.224.0 | 8,192 | 8,190 |
| /20 | 255.255.240.0 | 4,096 | 4,094 |
| /21 | 255.255.248.0 | 2,048 | 2,046 |
| /22 | 255.255.252.0 | 1,024 | 1,022 |
| /23 | 255.255.254.0 | 512 | 510 |
| /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 |
Questions
What is a /24 subnet?
A /24 is a block of 256 addresses whose first three octets are fixed, such as 192.168.1.0 to 192.168.1.255. Its mask is 255.255.255.0 and it has 254 usable hosts, because the first address names the network and the last is the broadcast.
How many hosts are in a /29?
A /29 has 8 addresses and 6 usable hosts. It is a common size for a small block of public IPv4 addresses on a dedicated server: one address for the gateway leaves 5 for your services.
What is a wildcard mask?
The subnet mask turned inside out: zeros where the mask has ones and ones where it has zeros. For a /24 it is 0.0.0.255. Cisco access lists and OSPF use it to say which bits of an address may vary.
What is the subnet mask for a /27?
255.255.255.224. A /27 holds 32 addresses with 30 usable hosts, so a /24 splits into eight /27 networks starting at .0, .32, .64 and so on.