Sub topic 8
Block size and subnet boundaries
This chapter assumes you can write the prefix length as a dotted-decimal mask, and that you know the network address is the first address of a subnet and the broadcast address is the last. Here you will turn that into a fast method. You find a subnet's block size from its mask, list the subnet boundaries, and place any address inside its subnet without writing binary.
What block size is
A subnet is a run of consecutive addresses. Its length is a power of two, and it starts at an address that is a multiple of that length. The length of the run, measured in the one octet where the mask stops, is the block size.
The method rests on one idea. In every octet to the left of the interesting octet, the mask is 255, so those octets never change inside a subnet. In every octet to the right, the mask is 0, so those octets run through their full range. Only the interesting octet (the last octet whose mask value is not 0) counts in the arithmetic.
The block size is:
block size = 256 - (mask value in the interesting octet)Equivalently, it is 2 raised to the number of host bits that fall in that octet. For example, /26 has the mask 255.255.255.192. The interesting octet is the fourth, and the block size is 256 - 192 = 64.
| Mask bits set in the interesting octet | Mask value | Block size | Prefixes that use it |
|---|---|---|---|
| 1 | 128 | 128 | /9, /17, /25 |
| 2 | 192 | 64 | /10, /18, /26 |
| 3 | 224 | 32 | /11, /19, /27 |
| 4 | 240 | 16 | /12, /20, /28 |
| 5 | 248 | 8 | /13, /21, /29 |
| 6 | 252 | 4 | /14, /22, /30 |
| 7 | 254 | 2 | /15, /23, /31 |
| 8 | 255 | 1 | /8, /16, /24, /32 |
To find the interesting octet from a prefix, divide by 8 and round up. A /20 gives 20 / 8 = 2.5, which rounds up to 3, so the third octet. A /24 gives exactly 3, so the third octet, with a block size of 1 there. In practice, people usually treat a /24 as "the whole fourth octet is host space".
Subnet boundaries are multiples of the block size
Subnet network addresses in the interesting octet are 0, then the block size, then twice the block size, and so on. With a block size of 64 in the fourth octet, the boundaries are 0, 64, 128, 192. Each subnet runs from its boundary to one below the next boundary.
For each subnet:
- Network address: the boundary itself.
- Broadcast address: the next boundary minus 1 in the interesting octet, with all octets to the right set to 255.
- Usable hosts: everything between the two, from network + 1 to broadcast - 1.
This gives the same result as the bitwise AND with the mask, but you only subtract and multiply small numbers.
Placing an address in its subnet
To find the subnet that contains a host address:
- Find the interesting octet and the block size.
- Divide the host's value in that octet by the block size and drop the remainder.
- Multiply the result by the block size. That is the network address's value in that octet.
- Copy the octets to the left unchanged. Set the octets to the right to 0.
- Add the block size minus 1 to the interesting octet and set the octets to the right to 255. That is the broadcast address.
Worked example: fourth octet
Find the subnet of 192.168.10.100/26. The block size is 64. 100 / 64 = 1 remainder 36, and 1 × 64 = 64. The network is 192.168.10.64, the broadcast is 192.168.10.127, and the usable range is .65 to .126.
Worked example: third octet
Find the subnet of 172.16.37.200/20. The mask is 255.255.240.0, so the interesting octet is the third, and the block size is 256 - 240 = 16. 37 / 16 = 2 remainder 5, and 2 × 16 = 32. The network is 172.16.32.0. The broadcast address is 172.16.47.255, because the third octet is 32 + 16 - 1 = 47 and the fourth octet is all ones. The fourth octet of the host (200) played no part, because every bit in it is a host bit.
The usable range is 172.16.32.1 to 172.16.47.254. That is 16 × 256 - 2 = 4094 hosts.
Worked example: a prefix inside the second octet
Find the subnet of 10.77.9.5/10. The mask is 255.192.0.0, and the block size is 64 in the second octet. 77 / 64 = 1, and 1 × 64 = 64. The network is 10.64.0.0, and the broadcast is 10.127.255.255.
Checking the method in code
The function below follows the five steps. It then compares the result with Python's ipaddress module, which uses full bit arithmetic.
import ipaddress
def block_math(addr, prefix):
octets = [int(x) for x in addr.split(".")]
i = (prefix - 1) // 8 # index of the interesting octet
block = 2 ** (8 - (prefix - 8 * i)) # block size in that octet
net = octets[:i] + [octets[i] // block * block] + [0] * (3 - i)
bc = octets[:i] + [net[i] + block - 1] + [255] * (3 - i)
return ".".join(map(str, net)), ".".join(map(str, bc))
cases = [("172.16.37.200", 20), ("10.1.77.9", 21), ("192.168.10.100", 26)]
for addr, prefix in cases:
net, bc = block_math(addr, prefix)
ref = ipaddress.ip_network(f"{addr}/{prefix}", strict=False)
same = net == str(ref.network_address) and bc == str(ref.broadcast_address)
print(f"{addr}/{prefix}", net, bc, same)
# 172.16.37.200/20 172.16.32.0 172.16.47.255 True
# 10.1.77.9/21 10.1.72.0 10.1.79.255 True
# 192.168.10.100/26 192.168.10.64 192.168.10.127 TrueThe strict=False argument tells ip_network to accept a host address and mask off the host bits. Without it, Python raises a ValueError for any address that is not a network boundary.
Aligned versus misaligned addresses
A network address must be a multiple of the block size in the interesting octet. This rule catches many errors.
192.168.10.64/26is a valid network address, because 64 is a multiple of 64.192.168.10.64/25is not a network address. The block size is 128, so the boundary below 64 is 0, and this address is a host inside192.168.10.0/25.10.1.20.0/21is not a network address. The block size is 8, and 20 is not a multiple of 8. The subnet that holds it starts at 16.
import ipaddress
print(ipaddress.ip_network("192.168.10.64/26")) # 192.168.10.64/26
print(ipaddress.ip_network("10.1.20.0/21", strict=False)) # 10.1.16.0/21
try:
ipaddress.ip_network("10.1.20.0/21")
except ValueError as err:
print(err) # 10.1.20.0/21 has host bits setSubnets that span several values of an octet
When the block size is larger than 1 in a non-final octet, the subnet covers many values of that octet. With /20, the third-octet block size is 16, and each subnet covers 16 values of the third octet times all 256 values of the fourth.
You can tell how many such subnets fit in a larger block by dividing. A /16 holds 2^(20-16) = 16 subnets of /20. Their third-octet boundaries are 0, 16, 32, 48, and so on up to 240.
Common errors
- Using the wrong octet. A
/20mask changes the third octet. Subtracting 240 from 256 in the fourth octet gives nonsense. - Off by one at the broadcast. The broadcast address is the next boundary minus 1, not the next boundary. In
192.168.10.0/26the broadcast is.63, not.64. - Forgetting the lower octets. In
172.16.32.0/20the broadcast is172.16.47.255. Writing172.16.47.0is wrong, because every octet to the right of the interesting octet becomes 255. - Rounding up instead of down. For 100 in blocks of 64, the answer is the boundary at or below the address, which is 64, not 128.
- Treating the address as a boundary. A host address such as
192.168.10.100is not a boundary. Use the division step.
Exercise
Find the network address, broadcast address and usable range of 10.1.77.9/21.
Show answer
The mask is 255.255.248.0. The interesting octet is the third, and the block size is 256 - 248 = 8. 77 / 8 = 9 remainder 5, and 9 × 8 = 72. The network is 10.1.72.0. The broadcast is 10.1.79.255, because 72 + 8 - 1 = 79. The usable range is 10.1.72.1 to 10.1.79.254.
Exercise
Is 192.168.50.96/27 a valid network address? If not, which network contains it?
Show answer
A /27 has a block size of 32 in the fourth octet (256 - 224). 96 = 3 × 32, so it is a multiple of 32 and the address is valid. The subnet runs from 192.168.50.96 to 192.168.50.127.
Exercise
How many /22 subnets fit in 172.20.0.0/16, and what are the third-octet values at which the first four start?
Show answer
A /22 uses a mask of 255.255.252.0, so the block size is 4 in the third octet. A /16 holds 2^(22-16) = 64 subnets of /22. The first four start at third-octet values 0, 4, 8 and 12, which are 172.20.0.0/22, 172.20.4.0/22, 172.20.8.0/22 and 172.20.12.0/22.