Sub topic 9
Splitting a network into equal subnets
This chapter assumes you can convert between binary and decimal for values 0 to 255. It also assumes you can read CIDR prefix lengths such as /24 and calculate network address, broadcast address, and usable hosts for a prefix. Here you use those skills to cut one address block into several same-sized pieces.
The core idea: borrowing host bits
An address block has a network part (the prefix) and a host part (the remaining bits). To make subnets, you extend the prefix by moving some bits from the host part to the network part. These are called borrowed bits.
Each borrowed bit doubles the number of subnets and halves the size of each one.
- Borrow 1 bit: 2 subnets.
- Borrow 2 bits: 4 subnets.
- Borrow 3 bits: 8 subnets.
- Borrow n bits: 2^n subnets.
The borrowed bits are the new "subnet number" field. Every combination of those bits names one subnet. The original prefix stays the same, and the host bits that remain identify devices inside each subnet.
The procedure
Use the same five steps every time.
- Decide how many subnets you need, or how many hosts each subnet needs.
- Find the number of borrowed bits. For n subnets, borrow the smallest b where 2^b ≥ n, which is ceil(log2 n).
- Add b to the original prefix length to get the new prefix length.
- Calculate the block size, which is the distance between the starts of consecutive subnets.
- List the subnets by adding the block size repeatedly, starting at the original network address.
The block size is 2^(32 − new prefix) addresses. When the new prefix ends inside an octet, it is easier to work in that octet. The block size there is 256 minus the mask's value in that octet. For example, /26 has mask 255.255.255.192, so the block size in the last octet is 256 − 192 = 64.
Reference table for a /24
| Bits borrowed | New prefix | Subnets | Block size | Usable hosts per subnet |
|---|---|---|---|---|
| 1 | /25 | 2 | 128 | 126 |
| 2 | /26 | 4 | 64 | 62 |
| 3 | /27 | 8 | 32 | 30 |
| 4 | /28 | 16 | 16 | 14 |
| 5 | /29 | 32 | 8 | 6 |
| 6 | /30 | 64 | 4 | 2 |
Usable hosts are 2^(host bits) − 2, because the first address is the network address and the last is the broadcast address.
Worked example: four subnets from a /24
Split 192.168.10.0/24 into 4 equal subnets.
- You need 4 subnets.
- 2^2 = 4, so borrow 2 bits.
- The new prefix is 24 + 2 = /26 (mask 255.255.255.192).
- The block size is 256 − 192 = 64.
- Start at .0 and add 64 each time.
| Subnet | First usable | Last usable | Broadcast |
|---|---|---|---|
| 192.168.10.0/26 | 192.168.10.1 | 192.168.10.62 | 192.168.10.63 |
| 192.168.10.64/26 | 192.168.10.65 | 192.168.10.126 | 192.168.10.127 |
| 192.168.10.128/26 | 192.168.10.129 | 192.168.10.190 | 192.168.10.191 |
| 192.168.10.192/26 | 192.168.10.193 | 192.168.10.254 | 192.168.10.255 |
Each subnet's broadcast address is one less than the next subnet's network address. The last subnet's broadcast address equals the original block's broadcast address. If these two checks fail, you made an arithmetic error.
The diagram shows that each borrowed bit is one level of the tree. Stopping after one level gives two /25 subnets. Stopping after two levels gives four /26 subnets.
Subnetting across an octet boundary
When the new prefix is not in the last octet, the block size applies to a different octet. Suppose you must split 172.16.0.0/16 into at least 6 subnets.
- ceil(log2 6) = 3 bits, since 2^3 = 8 ≥ 6.
- The new prefix is 16 + 3 = /19, with mask 255.255.224.0.
- The interesting octet is the third. The block size there is 256 − 224 = 32.
- The fourth octet is always 0 for the network address, and 255 for the broadcast address.
The eight subnets are:
- 172.16.0.0/19
- 172.16.32.0/19
- 172.16.64.0/19
- 172.16.96.0/19
- 172.16.128.0/19
- 172.16.160.0/19
- 172.16.192.0/19
- 172.16.224.0/19
The first subnet runs from 172.16.0.0 to 172.16.31.255. Its broadcast address is 172.16.31.255, one less than 172.16.32.0. Each subnet has 13 host bits, so it holds 2^13 − 2 = 8190 usable hosts.
You asked for 6 subnets but got 8. The two extra subnets are not an error. Keep them unused as reserve for growth.
Starting from a host requirement
Often the requirement is "each subnet needs h hosts" instead of a subnet count. Work from the host side:
- Find the host bits k where 2^k − 2 ≥ h.
- The new prefix is 32 − k.
- The number of subnets you get is 2^(new prefix − original prefix).
Example: each subnet in 192.168.1.0/24 must hold 50 hosts. 2^6 − 2 = 62 ≥ 50, so k = 6 and the prefix is /26. You get 2^(26−24) = 4 subnets.
Always check both requirements together. The host-count rule and the subnet-count rule can disagree. If you need 9 subnets of 20 hosts each from a /24, the subnet rule says borrow 4 bits (/28). The host rule says you need 5 host bits (/27). A /28 holds only 14 hosts and a /27 gives only 8 subnets, so the plan cannot work. You would need a larger parent block, or VLSM.
Doing it with Python
The ipaddress module performs the split directly. subnets(new_prefix=26) and subnets(prefixlen_diff=2) both produce the four /26 subnets.
import ipaddress
net = ipaddress.ip_network("192.168.10.0/24")
print(net.netmask) # 255.255.255.0
print(net.num_addresses) # 256
for sub in net.subnets(new_prefix=26):
print(sub, sub.network_address, sub.broadcast_address)
# 192.168.10.0/26 192.168.10.0 192.168.10.63
# 192.168.10.64/26 192.168.10.64 192.168.10.127
# 192.168.10.128/26 192.168.10.128 192.168.10.191
# 192.168.10.192/26 192.168.10.192 192.168.10.255The next script follows the procedure above. It takes a block and a subnet count, computes the borrowed bits, and prints each subnet with its usable range.
import ipaddress
import math
def split(block, count):
net = ipaddress.ip_network(block)
borrowed = math.ceil(math.log2(count))
if net.prefixlen + borrowed > 30:
raise ValueError("not enough host bits left for usable hosts")
print(f"borrow {borrowed} bits -> /{net.prefixlen + borrowed}, "
f"{2 ** borrowed} subnets")
for sub in net.subnets(prefixlen_diff=borrowed):
first = sub.network_address + 1
last = sub.broadcast_address - 1
usable = sub.num_addresses - 2
print(f"{sub} {first} - {last} broadcast {sub.broadcast_address} "
f"({usable} hosts)")
split("172.16.0.0/16", 6)
# borrow 3 bits -> /19, 8 subnets
# 172.16.0.0/19 172.16.0.1 - 172.16.31.254 broadcast 172.16.31.255 (8190 hosts)
# 172.16.32.0/19 172.16.32.1 - 172.16.63.254 broadcast 172.16.63.255 (8190 hosts)
# ... and six more, up to 172.16.224.0/19The guard rejects prefixes longer than /30, because the "subtract 2" rule does not hold for /31 and /32. Those special prefixes are covered in a later chapter.
Common errors
- Counting only the requested subnets. You get 2^b subnets, not n. Plan for the spare ones.
- Forgetting to subtract 2 from hosts. A /27 has 32 addresses and 30 usable hosts.
- Rounding down the borrowed bits. For 5 subnets, 2 bits give only 4. Always round up.
- Misaligned starting addresses. A subnet's network address must be a multiple of its block size. 192.168.10.32/26 is not a valid subnet start, because 32 is not a multiple of 64. The module raises a
ValueErrorfor it unless you passstrict=False. - Dropping the first or last subnet. Older "classful" guidance said to avoid the all-zeros and all-ones subnets. Modern equipment and CIDR allow them. Check older device configuration if an old router rejects them.
- Using the wrong octet for the block size. For /19 the block size is 32 in the third octet, not the fourth.
Quick check list
- Borrowed bits b = ceil(log2 subnets).
- New prefix = old prefix + b.
- Block size = 2^(32 − new prefix), or 256 − mask value in the interesting octet.
- Each subnet starts at a multiple of the block size.
- Broadcast = next network address − 1.
Exercise
You have 10.20.0.0/16 and need at least 30 equal subnets. What is the new prefix, the block size in the third octet, the number of usable hosts per subnet, and the network address of the fifth subnet?
Show answer
2^5 = 32 ≥ 30, so borrow 5 bits and the new prefix is /21 (mask 255.255.248.0). The block size in the third octet is 256 − 248 = 8. There are 11 host bits, so 2^11 − 2 = 2046 usable hosts. The subnets start at 10.20.0.0, 10.20.8.0, 10.20.16.0, 10.20.24.0, and 10.20.32.0, so the fifth subnet is 10.20.32.0/21.
Exercise
A team wants to split 192.168.5.0/24 into 9 equal subnets that each hold at least 20 hosts. Is that possible? Explain.
Show answer
No. 9 subnets need 4 borrowed bits (2^4 = 16 ≥ 9), giving /28 subnets with only 14 usable hosts. 20 hosts need 5 host bits (2^5 − 2 = 30 ≥ 20), which means /27 subnets, and a /24 yields only 8 of those. The two requirements conflict, so you need a larger parent block or unequal subnet sizes (VLSM).