Sub topic 8
Block size and subnet boundaries
This chapter assumes you can convert between binary and decimal, read a prefix length such as /26, and turn it into a subnet mask. It also assumes you know that the bitwise AND of an address and its mask gives the network address. Here you will find a subnet's boundaries with simple arithmetic and no binary conversion.
What block size is
A subnet's addresses form one contiguous run. The size of that run is the block size. Every subnet of that size starts at a multiple of it.
Block size is easiest to see in the interesting octet. This is the one octet where the mask is neither 255 nor 0. In that octet:
- Block size = 256 − the mask value in the interesting octet.
- Equivalently, block size = 2^(host bits in that octet).
| Prefix | Mask in interesting octet | Block size |
|---|---|---|
| /25 | 128 | 128 |
| /26 | 192 | 64 |
| /27 | 224 | 32 |
| /28 | 240 | 16 |
| /29 | 248 | 8 |
| /30 | 252 | 4 |
For prefixes such as /20, the interesting octet is the third one. The mask is 255.255.240.0, so the third-octet block size is 256 − 240 = 16. Subnets start at third-octet values 0, 16, 32, 48, and so on.
Finding the subnet that holds an address
Example 1. Which subnet contains 192.168.10.100/26?
- The mask is 255.255.255.192, so the interesting octet is the fourth and the block size is 64.
- The multiples of 64 are 0, 64, 128, and 192. The largest one that is at most 100 is 64.
- The network address is 192.168.10.64.
- The next boundary is 128, so the broadcast address is 192.168.10.127.
- The usable hosts run from 192.168.10.65 to 192.168.10.126.
Example 2. Which subnet contains 172.16.37.200/20?
- The mask is 255.255.240.0, so the interesting octet is the third and the block size is 16.
- The largest multiple of 16 that is at most 37 is 32.
- The network address is 172.16.32.0.
- The next boundary is 48, so the broadcast address is 172.16.47.255. Octets after the interesting one become 255.
Automating the same method
This script follows the manual steps and then checks the result against the standard library.
import ipaddress
addr = ipaddress.ip_address("172.16.37.200")
prefix = 20
i = (prefix - 1) // 8 # index of the interesting octet (0-3)
block = 2 ** (8 - (prefix - 8 * i)) # block size in that octet
octets = list(addr.packed)
start = octets[i] // block * block # round down to a multiple of block
net_octets = octets[:i] + [start] + [0] * (3 - i)
network = ipaddress.IPv4Address(bytes(net_octets))
check = ipaddress.ip_network((network, prefix))
print("block size:", block) # 16
print("network:", check) # 172.16.32.0/20
print("broadcast:", check.broadcast_address) # 172.16.47.255Common errors
- Using an address that is not on a boundary. 192.168.10.100/26 is a host address. As a network address it is invalid, because 100 is not a multiple of 64. Many tools reject it, and some silently round it down.
- Picking the wrong octet. A /20 changes the third octet, not the fourth. A mask of 255.255.240.0 has block size 16 in the third octet, and the fourth octet spans 0 to 255.
- Forgetting the lower octets. When the interesting octet is the third, the network address ends in .0 and the broadcast address ends in .255.
- Off-by-one on the broadcast. The broadcast address is the next boundary minus 1, not the next boundary.
Exercise
A host has the address 10.4.77.9/21. Give its network address, broadcast address, and the block size you used.
Show answer
A /21 mask is 255.255.248.0, so the third octet is interesting and the block size is 256 − 248 = 8. The largest multiple of 8 that is at most 77 is 72. The network address is 10.4.72.0. The next boundary is 80, so the broadcast address is 10.4.79.255. The block size is 8.