Sub topic 7
Network address, broadcast address, and usable hosts
This chapter assumes you can convert between dotted decimal and binary, and that you know what a subnet mask and a prefix length are. Here you will use them to find the first address, the last address, and the number of usable hosts in a subnet.
The two reserved addresses
Every IPv4 subnet reserves two addresses that cannot be given to a host.
- The network address has all host bits set to 0. It names the subnet itself, for example
192.168.1.0/24. - The broadcast address has all host bits set to 1. A packet sent to it is delivered to every host in the subnet, for example
192.168.1.255.
Every address between them is usable by a host. With h host bits, the subnet holds 2^h addresses, so the usable count is 2^h − 2.
Worked example: 192.168.1.0/24
A /24 prefix leaves 32 − 24 = 8 host bits.
- Total addresses: 2^8 = 256.
- Network address: all host bits 0, so
192.168.1.0. - Broadcast address: all host bits 1, so
192.168.1.255. - Usable hosts: 256 − 2 = 254, from
192.168.1.1to192.168.1.254.
import ipaddress
net = ipaddress.ip_network("192.168.1.0/24")
print(net.netmask) # 255.255.255.0
print(net.network_address) # 192.168.1.0
print(net.broadcast_address) # 192.168.1.255
print(net.num_addresses - 2) # 254 usable host addressesFinding the network address from any host address
Hosts rarely come with the network address written out. You usually have a host address and a mask, and you compute the network address with a bitwise AND of the two. Where the mask has a 1, the address bit is kept. Where the mask has a 0, the result is 0.
Take 192.168.1.77/26. The mask is 255.255.255.192, and only the last octet matters.
- 77 in binary is
01001101. - 192 in binary is
11000000. - AND gives
01000000, which is 64.
The network address is 192.168.1.64. The subnet has 6 host bits, so it holds 64 addresses. The broadcast address is 192.168.1.127, and the usable range is 192.168.1.65 to 192.168.1.126, which is 62 hosts.
import ipaddress
iface = ipaddress.ip_interface("192.168.1.77/26")
net = iface.network
print(net.network_address) # 192.168.1.64
print(net.broadcast_address) # 192.168.1.127
print(net.num_addresses - 2) # 62Quick reference
| Prefix | Host bits | Total addresses | Usable hosts |
|---|---|---|---|
| /24 | 8 | 256 | 254 |
| /26 | 6 | 64 | 62 |
| /28 | 4 | 16 | 14 |
| /30 | 2 | 4 | 2 |
Common errors
- Forgetting to subtract 2. A /28 holds 16 addresses but only 14 can be assigned to hosts.
- Assuming the network address always ends in
.0. In192.168.1.64/26it ends in.64, and the broadcast address does not end in.255. - Assigning the network or broadcast address to a host. Devices may reject it, or traffic may behave unpredictably.
Exercise
A host has the address 10.4.9.200/27. What are the network address, the broadcast address, and the number of usable hosts?
Show answer
A /27 leaves 5 host bits, so each subnet has 32 addresses and the mask in the last octet is 224. 200 AND 224 = 192 (11001000 AND 11100000 = 11000000). The network address is 10.4.9.192. The broadcast address is 192 + 31 = 223, so 10.4.9.223. The usable hosts are 32 − 2 = 30, from 10.4.9.193 to 10.4.9.222.
Exercise
Why can a /30 subnet be used for a link between two routers, but a /29 wastes addresses on that link?
Show answer
A /30 has 4 addresses, of which 2 are usable, which is exactly the two router interfaces. A /29 has 8 addresses and 6 usable, so 4 of them go unused on a link that needs only 2.