Sub topic 7
Network Address, Broadcast Address, and Usable Hosts
This chapter assumes you can read an IPv4 address in binary, and that you know a subnet mask or CIDR prefix marks where the network part ends and the host part begins. If those ideas are new, review the earlier chapters on address structure and prefix length first. The code uses Python, but the arithmetic works on paper.
Why every subnet loses two addresses
A subnet is a block of consecutive addresses that share the same network bits. Inside that block, two addresses are reserved by convention and are not assigned to ordinary hosts:
- The network address has every host bit set to 0. It names the subnet itself, and routing tables use it as the destination for the whole block.
- The broadcast address has every host bit set to 1. A packet sent to it is delivered to every host on that subnet. This is the directed broadcast address.
Every address between those two can be assigned to a host. If a subnet has h host bits, it holds 2^h addresses, and the usable count is:
usable hosts = 2^h - 2 where h = 32 - prefix lengthFor a /24, h is 8, so 2^8 - 2 = 254 usable addresses. For a /26, h is 6, so 2^6 - 2 = 62.
Finding both addresses with bit operations
You can derive both special addresses from any address in the subnet and its mask, using two bitwise operations:
- Network address = address AND mask. The AND clears every host bit to 0 and keeps the network bits.
- Broadcast address = network address OR (NOT mask). The inverted mask has 1s only in the host positions, so the OR sets every host bit to 1.
Take the host 192.168.1.77 with the prefix /26. The mask is 255.255.255.192. The first three octets are all-ones in the mask, so they pass through unchanged. Only the last octet needs work:
| Step | Last octet in binary | Decimal |
|---|---|---|
| Address | 01001101 | 77 |
| Mask | 11000000 | 192 |
| Address AND mask (network) | 01000000 | 64 |
| NOT mask | 00111111 | 63 |
| Network OR NOT mask (broadcast) | 01111111 | 127 |
The subnet is 192.168.1.64/26. Its broadcast address is 192.168.1.127. The usable range is 192.168.1.65 through 192.168.1.126, which is 62 addresses.
The same calculation in Python
This function implements the two bit operations directly. It converts the address to a 32-bit integer, builds the mask from the prefix length, and converts the results back to dotted decimal.
def to_int(address):
a, b, c, d = (int(part) for part in address.split("."))
return (a << 24) | (b << 16) | (c << 8) | d
def to_text(value):
return ".".join(str((value >> shift) & 255) for shift in (24, 16, 8, 0))
def subnet_details(address, prefix):
mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
network = to_int(address) & mask
broadcast = network | (~mask & 0xFFFFFFFF)
usable = max(2 ** (32 - prefix) - 2, 0)
return to_text(network), to_text(broadcast), usable
print(subnet_details("192.168.1.77", 26)) # ('192.168.1.64', '192.168.1.127', 62)
print(subnet_details("172.16.5.4", 22)) # ('172.16.4.0', '172.16.7.255', 1022)
print(subnet_details("10.0.0.9", 8)) # ('10.0.0.0', '10.255.255.255', 16777214)The & 0xFFFFFFFF after the shift keeps the mask at 32 bits, because Python integers have no fixed width. The same masking after ~mask matters because ~ on a Python integer produces a negative number.
In real code, prefer the standard library. The ipaddress module does the same work and validates the input:
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 addresses
hosts = list(ipaddress.ip_network("192.168.1.64/26").hosts())
print(hosts[0], hosts[-1], len(hosts)) # 192.168.1.65 192.168.1.126 62ip_network raises ValueError when host bits are set, as in 192.168.1.77/26. Pass strict=False to have it round down to the real network address. Use ip_interface("192.168.1.77/26") when you need to keep the host address together with its prefix.
Block size shortcut
You can find the boundaries without binary. Find the interesting octet, which is the one where the mask is neither 255 nor 0. Compute the block size as 256 minus the mask value in that octet. Subnets start at multiples of the block size.
For a /26, the mask octet is 192, so the block size is 64. Subnets start at .0, .64, .128, and .192. The address .77 falls in the block starting at 64. The broadcast address is the next block start minus 1, which is 127.
The same method works when the interesting octet is not the last one. For 172.16.5.4/22, the mask is 255.255.252.0, so the third octet is the interesting one, and the block size is 256 - 252 = 4. Third-octet values 4, 5, 6, and 7 form one block. The network address is 172.16.4.0 and the broadcast address is 172.16.7.255. Notice that the broadcast address ends in .255 here but is not the end of a /24. Do not assume the last octet of a broadcast address is always 255.
Quick reference for common prefixes
| Prefix | Mask | Total addresses | Usable hosts |
|---|---|---|---|
| /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 |
Each extra prefix bit halves the total address count. Because the two reserved addresses are a fixed cost, small subnets lose a larger share. A /30 wastes half its addresses, while a /24 wastes under 1%.
Edge cases: /31 and /32
The formula 2^h - 2 breaks down at the smallest prefixes.
- /32 has h = 0. It contains exactly one address, with no network or broadcast address to set aside. It identifies a single host. You see /32 routes for loopback interfaces and for individual host routes. The formula gives -1, which is meaningless.
- /31 has h = 1. The formula gives 0 usable hosts. RFC 3021 defines a different rule for point-to-point links: both addresses are assigned to the two ends of the link, and there is no network or broadcast address. This saves one address per link compared with a /30. Many routers support this, but older equipment or some operating systems may not. Check before you deploy.
import ipaddress
link = ipaddress.ip_network("10.0.0.0/31")
print([str(h) for h in link.hosts()]) # ['10.0.0.0', '10.0.0.1']
single = ipaddress.ip_network("10.0.0.5/32")
print([str(h) for h in single.hosts()]) # ['10.0.0.5']Note that hosts() follows the RFC 3021 rule for /31 and returns both addresses. This differs from num_addresses - 2, which gives 0, so check the prefix length before applying that subtraction.
Common errors
- Assigning the network or broadcast address to a host. A host configured as 192.168.1.64/26 uses the subnet's own name as its address. A host configured as 192.168.1.127/26 receives broadcast traffic as if it were addressed to itself. Neither works reliably.
- Forgetting that the answer depends on the prefix. 192.168.1.255 is a broadcast address in 192.168.1.0/24, but it is an ordinary usable host in 192.168.0.0/23, which spans 192.168.0.0 to 192.168.1.255 and has its broadcast at 192.168.1.255. Wait, in that case it is also the broadcast. A better example is 192.168.1.255 in a /25-sized block 192.168.1.128/25, where it is again the broadcast. The reliable point is that you cannot tell from the last octet alone. In 10.0.0.0/16, the address 10.0.1.255 is a perfectly normal host, because the host bits are not all 1s.
- Using the class default mask by habit. Under CIDR, 10.0.1.0/24 is a normal subnet even though 10.x.x.x is class A. Always take the mask from the configuration, not from the first octet.
- Off-by-one errors in ranges. The last usable address is the broadcast address minus 1, and the first usable address is the network address plus 1.
Exercise
A host is configured as 10.1.1.200/28. Give the network address, the broadcast address, the first and last usable addresses, and the number of usable hosts.
Show answer
The mask is 255.255.255.240, so the block size is 256 - 240 = 16. Blocks in the last octet start at 0, 16, ..., 192, 208. The value 200 lies in the block starting at 192. Network address: 10.1.1.192. Broadcast address: 10.1.1.207. First usable: 10.1.1.193. Last usable: 10.1.1.206. Usable hosts: 2^4 - 2 = 14.
Exercise
An administrator assigns 192.168.10.63/26 to a server. Why is this a mistake, and what is the nearest valid addresses on either side?
Show answer
A /26 has a block size of 64, so the block containing 63 runs from 192.168.10.0 to 192.168.10.63. The address .63 is the broadcast address of that subnet, because all six host bits are 1. It cannot be assigned to a host. The last valid host in that subnet is 192.168.10.62. The next subnet starts at 192.168.10.64, which is its network address, so the first valid host there is 192.168.10.65.
Exercise
Why does a /30 subnet waste half its addresses, and how does a /31 avoid this on a point-to-point link?
Show answer
A /30 has 4 addresses: the network address, two usable hosts, and the broadcast address. Two of the four are reserved. On a point-to-point link only two hosts exist, and a link has no use for a broadcast address. RFC 3021 lets a /31 assign both of its two addresses to the two link ends, with no network or broadcast address. That uses 2 addresses per link instead of 4.