Before you start
- Know how to read a mask and a prefix: subnet masks and CIDR notation
- Keep the subnet mask cheat sheet at hand
In plain words
A network is a continuous run of addresses. Calculating a subnet means finding where that run starts (the network address), where it ends (the broadcast address) and what lies in between (the host addresses). One trick is enough: networks always start on a multiple of the same number, the block size.
What we are looking for
For a given address with its prefix, for example 192.168.10.77/27, we want six pieces of information:
| Information | Definition |
|---|---|
| Mask | The prefix written in dotted-decimal notation |
| Network address | The first address of the range; it identifies the network itself |
| Broadcast address | The last address; it reaches every host on the network |
| First host | Network address + 1 |
| Last host | Broadcast address − 1 |
| Number of hosts | 2^(32 − prefix) − 2 |
1. The block-size method (the fastest)
You can do it in your head, in five steps. Example: 192.168.10.77/27.
Step 1: find the interesting octet. It is the mask octet that is neither 255 nor 0 (or the last 255 if the prefix is a multiple of 8). A /27 gives 255.255.255.224: it is the 4th octet, worth 224.
Step 2: compute the block size. Block size = 256 − 224 = 32.
Step 3: list the multiples of the block size in that octet: 0, 32, 64, 96, 128…
Step 4: bracket the address. The 4th octet of the address is 77. It lies between 64 and 96. So the network starts at 64, and the next network at 96.
Step 5: work out the rest.
| Information | Result |
|---|---|
| Mask | 255.255.255.224 |
| Network address | 192.168.10.64 |
| Broadcast address | 192.168.10.95 (96 − 1) |
| First host | 192.168.10.65 |
| Last host | 192.168.10.94 |
| Number of hosts | 2⁵ − 2 = 30 |
Tip
Octets before the interesting octet are copied as they are. Octets after it are 0 in the network address and 255 in the broadcast address. This matters when the block size falls on the 2nd or 3rd octet (examples 2 and 3).
2. The binary method (to understand and check)
This is what the machine actually does. We only look at the interesting octet, here the 4th.
Network address: a bitwise logical AND between the address and the mask. A bit stays at 1 only if it is 1 on both sides.
Address 77 0100 1101
Mask 224 1110 0000
---------
AND 0100 0000 = 64 → network 192.168.10.64
Broadcast address: keep the network bits, and set every host bit (the 0s of the mask) to 1.
Mask 224 111 00000 (3 network bits, 5 host bits)
Network 64 010 00000
Broadcast 95 010 11111 → broadcast 192.168.10.95
Both methods give the same result: the block size is just a shortcut for the binary calculation.
3. Example 2: the block size on the 3rd octet
Address: 172.16.45.200/20.
- Mask of a /20:
255.255.240.0. The interesting octet is the 3rd (240). - Block size = 256 − 240 = 16.
- Multiples of 16: 0, 16, 32, 48, 64…
- The 3rd octet of the address is 45: it lies between 32 and 48.
- Apply the rule from the tip: the 4th octet is 0 for the network and 255 for the broadcast.
| Information | Result |
|---|---|
| Mask | 255.255.240.0 |
| Network address | 172.16.32.0 |
| Broadcast address | 172.16.47.255 |
| First host | 172.16.32.1 |
| Last host | 172.16.47.254 |
| Number of hosts | 2¹² − 2 = 4,094 |
Warning
Here, 172.16.40.0 or 172.16.47.0 are perfectly valid host addresses, even though they end in 0. Only the very first (172.16.32.0) and the very last (172.16.47.255) addresses of the network are reserved.
4. Example 3: the block size on the 2nd octet
Address: 10.5.130.9/12.
- Mask of a /12:
255.240.0.0. The interesting octet is the 2nd (240). - Block size = 16.
- Multiples of 16: 0, 16, 32…
- The 2nd octet is 5: it lies between 0 and 16.
- The 3rd and 4th octets are 0 for the network and 255 for the broadcast.
| Information | Result |
|---|---|
| Mask | 255.240.0.0 |
| Network address | 10.0.0.0 |
| Broadcast address | 10.15.255.255 |
| First host | 10.0.0.1 |
| Last host | 10.15.255.254 |
| Number of hosts | 2²⁰ − 2 = 1,048,574 |
5. Two questions people often ask
Are these two machines on the same network?
192.168.10.77/27 and 192.168.10.100/27? With a block size of 32, the first is in the network starting at 64 (64 to 95), the second in the one starting at 96 (96 to 127). Answer: no. They cannot talk directly: they need to go through a router.
Which prefix for N hosts?
Look for the smallest network whose number of hosts is at least N. Example for 50 hosts:
| Prefix | Hosts | Enough? |
|---|---|---|
| /27 | 30 | No |
| /26 | 62 | Yes |
Leave some headroom: the gateway and network devices take addresses too, and networks only ever grow.
What about /31 and /32?
The "− 2" formula does not apply to them. A /32 means a single address (a specific machine, in a firewall rule or a route). A /31 holds two addresses, both usable, with no network or broadcast address: it is used for point-to-point links between two routers (RFC 3021).
6. Check with a tool
Once you have done the calculation by hand, a tool can confirm it.
ipcalc (Linux, macOS with Homebrew)
sudo apt install ipcalc
ipcalc 192.168.10.77/27
Excerpt of the output:
Network: 192.168.10.64/27
HostMin: 192.168.10.65
HostMax: 192.168.10.94
Broadcast: 192.168.10.95
Hosts/Net: 30
On macOS, install it with brew install ipcalc.
Python (all systems)
Python ships with a module dedicated to IP addresses. This line works on Linux, macOS and Windows (replace python3 with py on Windows):
python3 -c "import ipaddress; n=ipaddress.ip_interface('192.168.10.77/27').network; print(n, n.broadcast_address, n.num_addresses-2)"
Result:
192.168.10.64/27 192.168.10.95 30
7. Practice
| Address | Network | Broadcast | Hosts |
|---|---|---|---|
192.168.1.200/26 |
192.168.1.192 |
192.168.1.255 |
62 |
192.168.50.33/28 |
192.168.50.32 |
192.168.50.47 |
14 |
10.10.10.10/30 |
10.10.10.8 |
10.10.10.11 |
2 |
172.20.130.4/18 |
172.20.128.0 |
172.20.191.255 |
16,382 |
10.200.5.6/14 |
10.200.0.0 |
10.203.255.255 |
262,142 |
Key takeaways
- Block size = 256 − the value of the interesting octet in the mask.
- The network starts on the multiple of the block size just below the address; the broadcast is just before the next multiple.
- Octets after the interesting octet are 0 (network) and 255 (broadcast).
- First host = network + 1, last host = broadcast − 1.
- Check with
ipcalcor Python'sipaddressmodule.