Sub topic 7
Network address, broadcast address, and usable hosts
Every IPv4 subnet reserves two addresses for itself: the first and the last. This chapter shows how to find those two addresses for any prefix, and how to count the addresses left for devices. It assumes you can convert numbers from 0 to 255 between binary and decimal, and that you know a CIDR prefix such as /26 gives the number of network bits. It also assumes you know what a subnet mask is, as covered in the previous chapter.
The three roles inside a subnet
Every subnet is a contiguous range of addresses. The host bits (the bits to the right of the prefix) decide where an address sits in that range, and they give each address one of three roles.
- Network address: all host bits are 0. It names the subnet itself, as in the routing-table entry
192.168.10.64/26. You never assign it to a device. - Broadcast address: all host bits are 1. A packet sent to it is delivered to every host in the subnet. This is the directed broadcast address. You never assign it to a device either.
- Usable host addresses: everything between the two. These go to interfaces, such as hosts, router ports, and servers.
If a prefix has h host bits (h = 32 - prefix), the subnet holds 2^h addresses in total. The usable count is 2^h - 2.
| Prefix | Host bits | Total addresses | Usable hosts |
|---|---|---|---|
| /8 | 24 | 16,777,216 | 16,777,214 |
| /16 | 16 | 65,536 | 65,534 |
| /24 | 8 | 256 | 254 |
| /25 | 7 | 128 | 126 |
| /26 | 6 | 64 | 62 |
| /27 | 5 | 32 | 30 |
| /28 | 4 | 16 | 14 |
| /29 | 3 | 8 | 6 |
| /30 | 2 | 4 | 2 |
Each extra prefix bit halves the total. Because the two reserved addresses are fixed, the usable count falls by slightly more than half: from 254 to 126, then to 62, and so on.
Finding the addresses with bitwise logic
Two operations give you both reserved addresses.
- Network address = IP address AND subnet mask. The AND keeps the network bits and clears the host bits.
- Broadcast address = network address OR wildcard mask. The wildcard mask is the mask inverted (host bits all 1). The OR sets every host bit to 1.
Take 192.168.10.77/26. The mask is 255.255.255.192. Only the last octet is interesting, because the first three are all 1s in the mask and pass through unchanged.
| Step | Last octet in binary | Decimal |
|---|---|---|
| Address | 01001101 | 77 |
| Mask | 11000000 | 192 |
| AND (network) | 01000000 | 64 |
| Wildcard | 00111111 | 63 |
| OR (broadcast) | 01111111 | 127 |
The results:
- Network address:
192.168.10.64 - Broadcast address:
192.168.10.127 - First usable host:
192.168.10.65 - Last usable host:
192.168.10.126 - Total addresses: 64, so usable hosts: 62
A shortcut when the boundary falls inside an octet
Binary is reliable, but you can also do the work in decimal. Find the octet where the prefix ends. That octet is the "interesting" one. The block size is 256 - mask value in that octet. The network address is the largest multiple of the block size that does not exceed the address's value in that octet. The broadcast address is one less than the next multiple.
Example: 172.16.37.200/20. The mask is 255.255.240.0, so the interesting octet is the third, with mask value 240. The block size is 256 - 240 = 16. The multiples of 16 near 37 are 32 and 48, so the network address is 172.16.32.0. The next block starts at 48, so the broadcast address is 172.16.47.255. Every octet after the interesting one becomes 0 in the network address and 255 in the broadcast address.
The usable range is 172.16.32.1 to 172.16.47.254. The host bits number 12, so there are 2^12 - 2 = 4,094 usable hosts.
The next chapter develops block size in full. Here it is only a way to check the bitwise answer.
Computing it in code
The ipaddress module does the arithmetic. The function below also handles the two prefixes where the "minus 2" rule does not apply, /31 and /32, which are covered in the next section.
import ipaddress
def describe(cidr):
iface = ipaddress.ip_interface(cidr)
net = iface.network
total = net.num_addresses
if net.prefixlen == 32:
first = last = net.network_address
usable = 1
elif net.prefixlen == 31:
first = net.network_address
last = net.broadcast_address
usable = 2
else:
first = net.network_address + 1
last = net.broadcast_address - 1
usable = total - 2
print(f"{iface} -> network {net.network_address}, "
f"first {first}, last {last}, "
f"broadcast {net.broadcast_address}, "
f"total {total}, usable {usable}")
for cidr in ["192.168.10.77/26", "10.0.0.0/8", "172.16.37.200/20",
"10.1.1.4/31", "10.1.1.9/32"]:
describe(cidr)
# 192.168.10.77/26 -> network 192.168.10.64, first 192.168.10.65, last 192.168.10.126, broadcast 192.168.10.127, total 64, usable 62
# 10.0.0.0/8 -> network 10.0.0.0, first 10.0.0.1, last 10.255.255.254, broadcast 10.255.255.255, total 16777216, usable 16777214
# 172.16.37.200/20 -> network 172.16.32.0, first 172.16.32.1, last 172.16.47.254, broadcast 172.16.47.255, total 4096, usable 4094
# 10.1.1.4/31 -> network 10.1.1.4, first 10.1.1.4, last 10.1.1.5, broadcast 10.1.1.5, total 2, usable 2
# 10.1.1.9/32 -> network 10.1.1.9, first 10.1.1.9, last 10.1.1.9, broadcast 10.1.1.9, total 1, usable 1ip_interface accepts an address that is not on a subnet boundary, like 192.168.10.77/26, and its .network attribute applies the AND for you. ip_network would raise a ValueError for the same input, because host bits are set. Use ip_network(cidr, strict=False) if you want the same leniency there.
Edge cases
/31 point-to-point links. A link between two routers has only two ends. Under the "minus 2" rule a /31 would have zero usable hosts. RFC 3021 changes the rule for point-to-point links. Both addresses of a /31 are usable, and there is no broadcast address. This saves half the addresses compared with a /30. Not every device supports it, so check before using it on mixed equipment.
/32 host routes. A /32 is a single address. It is used for loopback interfaces, for router IDs, and for routes that point at one specific host. There are no host bits, so there is no separate network or broadcast address.
/30 links. On older equipment that does not support /31, a /30 is the smallest subnet that works for a point-to-point link: the network address, two usable hosts, and the broadcast address.
Directed broadcast versus limited broadcast. The broadcast address of a subnet, such as 192.168.10.127, is a directed broadcast. The address 255.255.255.255 is the limited broadcast, which stays on the local link. Routers normally do not forward directed broadcasts by default, because forwarding them enables amplification attacks.
Sizing a subnet from a host count
To work backwards from a requirement, find the smallest h such that 2^h - 2 is at least the number of hosts you need. For 100 hosts, 2^6 - 2 = 62 is too small and 2^7 - 2 = 126 is enough. So h = 7 and the prefix is 32 - 7 = /25. Round up, never down. A /25 leaves 26 spare addresses for growth, which is usually acceptable.
If you plan to allow for growth, add it to the requirement before you compute h. Changing a subnet's size after hosts are configured means renumbering them.
Exercise
A host has the address 10.4.9.130/27. Give the network address, broadcast address, usable host range, and number of usable hosts.
Show answer
A /27 has 5 host bits, so the block size is 32 and the mask is 255.255.255.224. The last octet is 130, and the largest multiple of 32 not above 130 is 128. Network address: 10.4.9.128. The next block starts at 160, so the broadcast address is 10.4.9.159. Usable range: 10.4.9.129 to 10.4.9.158. Usable hosts: 2^5 - 2 = 30.
Exercise
A department needs 500 hosts in one subnet. What is the longest prefix (smallest subnet) that fits, and how many usable addresses does it have?
Show answer
You need 2^h - 2 >= 500. With h = 8 the result is 254, which is too small. With h = 9 the result is 510, which fits. The prefix is 32 - 9 = /23, with 510 usable addresses and 10 spare.