Sub topic 5
CIDR Notation and Prefix Length
This chapter assumes you can convert values from 0 to 255 between binary and decimal. It also assumes you know that an IPv4 address has 32 bits, written as four octets, and that those bits split into a network part and a host part. The chapter covers how CIDR writes that split as a single number and how to move between prefix lengths and masks.
From classes to prefixes
Classful addressing fixed the network/host boundary at /8, /16, or /24, based on the first bits of the address. A company that needed 300 addresses had to take a Class B with 65,534 usable hosts, or several Class C networks. That wasted address space and filled routing tables with many small entries.
Classless Inter-Domain Routing (CIDR) removed the fixed boundaries. The network/host boundary can sit after any of the 32 bits. CIDR was introduced in the early 1990s (RFC 1519, later updated by RFC 4632). Today an address's first bits no longer tell you its mask. You must be told the mask, and CIDR notation is the compact way to give it.
What a prefix length is
The prefix length is the number of leading bits that identify the network. It is written after the address with a slash:
192.168.10.77/26Here /26 means the first 26 bits are the network portion. The remaining 32 − 26 = 6 bits are the host portion.
The address together with its prefix length gives you everything you need. The same address can belong to very different networks depending on the prefix:
| Notation | Network bits | Host bits | Addresses in block |
|---|---|---|---|
| 10.1.2.3/8 | 8 | 24 | 16,777,216 |
| 10.1.2.3/16 | 16 | 16 | 65,536 |
| 10.1.2.3/24 | 24 | 8 | 256 |
| 10.1.2.3/30 | 30 | 2 | 4 |
The number of addresses in a block is 2 raised to the number of host bits, so a /n prefix gives 2^(32−n) addresses.
Prefix length and subnet mask
A subnet mask is a 32-bit number with ones in the network positions and zeros in the host positions. A /n prefix is the same as a mask with n ones followed by 32 − n zeros. The ones must be contiguous. A value such as 255.0.255.0 is not a valid CIDR mask.
For /26:
11111111.11111111.11111111.11000000
255 . 255 . 255 . 192So /26 is 255.255.255.192.
Converting a prefix to a mask
Work octet by octet:
- Divide the prefix length by 8. The whole part is the number of octets that are 255.
- The remainder r is the number of leading ones in the next octet.
- Any remaining octets are 0.
For the partial octet, the value is the sum of the first r bit weights from the left. The weights are 128, 64, 32, 16, 8, 4, 2, 1.
| Leading ones in octet | Octet value |
|---|---|
| 0 | 0 |
| 1 | 128 |
| 2 | 192 |
| 3 | 224 |
| 4 | 240 |
| 5 | 248 |
| 6 | 252 |
| 7 | 254 |
| 8 | 255 |
Worked example, /20: 20 ÷ 8 = 2 remainder 4. Two octets are 255, the third has 4 leading ones (240), and the last is 0. The mask is 255.255.240.0.
Converting a mask to a prefix
Count the one bits. For 255.255.255.224, the first three octets give 24 ones. 224 is 11100000, which adds 3. The prefix is /27.
A quick check that a mask is valid: in binary, once you see a 0, no 1 may follow.
Finding the network address
The network address is the address with all host bits set to 0. You get it with a bitwise AND of the address and the mask. Each result bit is 1 only if both input bits are 1.
Take 192.168.10.77/26. Only the last octet is affected, because the first three octets of the mask are 255.
77 = 01001101
192 = 11000000
AND = 01000000 = 64The network address is 192.168.10.64/26. Setting all six host bits to 1 gives 01111111 = 127, so the broadcast address is 192.168.10.127. Later chapters treat address counts and usable hosts in detail. Here the point is the mechanism: the prefix length decides which bits the AND keeps.
The following program does this for any address and prefix:
import ipaddress
ip = int(ipaddress.ip_address("172.16.37.200"))
prefix = 20
mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
network = ip & mask
broadcast = network | (~mask & 0xFFFFFFFF)
print(ipaddress.ip_address(mask)) # 255.255.240.0
print(ipaddress.ip_address(network)) # 172.16.32.0
print(ipaddress.ip_address(broadcast)) # 172.16.47.255The shift 0xFFFFFFFF << (32 - prefix) puts prefix ones at the left. The & 0xFFFFFFFF trims the result back to 32 bits, because Python integers do not overflow.
Prefix length as a sliding boundary
Each step of one in the prefix length moves the boundary by one bit. Making the prefix longer (a larger number) converts a host bit into a network bit. The block halves in size. Making it shorter doubles the block.
- Going from /24 to /25 splits one block of 256 into two blocks of 128.
- Going from /24 to /23 merges two adjacent /24 blocks into one block of 512, provided they are aligned.
This is why a larger prefix number means a smaller network. People often get this backwards, because the number is bigger but the network is smaller.
Which octet is interesting
The boundary falls inside one octet unless the prefix is a multiple of 8. That octet is called the interesting octet. Everything to its left is copied from the address, and everything to its right is 0 in the network address.
| Prefix range | Interesting octet |
|---|---|
| /1 to /8 | First |
| /9 to /16 | Second |
| /17 to /24 | Third |
| /25 to /32 | Fourth |
For 172.16.37.200/20, the third octet is interesting. Its mask value is 240, so the block size in that octet is 256 − 240 = 16. The multiples of 16 are 0, 16, 32, 48, and so on. 37 falls between 32 and 48, so the network is 172.16.32.0 and the block ends at 172.16.47.255. This agrees with the program above. The next chapters build on this block-size method.
Common errors
- Reading the prefix as a host count. /26 means 26 network bits, not 26 hosts. It leaves 6 host bits, so the block has 64 addresses.
- Using a mask that is not contiguous. Masks such as 255.255.0.255 are not valid in CIDR.
- Forgetting alignment. A /26 block must start at a multiple of 64 in the last octet. 192.168.10.10/26 is a host address inside 192.168.10.0/26, not a network address.
- Mixing up the network and the interface address. A router interface might be configured as 192.168.10.77/26. The /26 describes the network the interface is on, not a separate network.
- Assuming the class decides the mask. 10.1.2.3/24 is valid. The first octet being 10 does not force /8.
- Confusing mask and wildcard. A wildcard mask, used in some access lists, is the inverse of the subnet mask. For /26 the wildcard is 0.0.0.63. Do not use one where the other is expected.
The next program shows all of these forms:
import ipaddress
iface = ipaddress.ip_interface("192.168.10.77/26")
print(iface.ip) # 192.168.10.77
print(iface.network) # 192.168.10.64/26
print(iface.netmask) # 255.255.255.192
print(iface.with_hostmask) # 192.168.10.77/0.0.0.63
net = ipaddress.ip_network("192.168.10.77/26", strict=False)
print(net.prefixlen) # 26
print(net.num_addresses) # 64Reading prefixes in practice
Prefix lengths appear in many places, with some conventions worth knowing:
- Routing tables list destinations as prefixes, such as
10.20.0.0/16. - Firewall and cloud security rules use them to define source ranges.
0.0.0.0/0matches every address, and a/32matches exactly one. - Interface configuration on most systems accepts either a mask or a prefix length. Linux
ip addr add 192.168.10.77/26 dev eth0uses the prefix form. - The prefix length is the sum of ones in the mask, so a longer prefix is a more specific match. This idea returns when you study routing decisions.
Exercise
A host is configured as 10.45.200.131/21. What are the subnet mask, the network address, and the last address in the block?
Show answer
A /21 gives 21 ÷ 8 = 2 remainder 5, so the first two octets are 255 and the third has 5 leading ones, which is 248. The mask is 255.255.248.0. The third octet is interesting, with block size 256 − 248 = 8. The multiples of 8 are ..., 192, 200, 208. The value 200 is itself a multiple of 8, so the network address is 10.45.200.0. The block ends just before 208, so the last address is 10.45.207.255.
Exercise
Is 255.255.255.100 a valid subnet mask? If the mask is 255.255.255.248, what is the prefix length, and how many addresses does the block contain?
Show answer
100 in binary is 01100100, which has a 0 before some 1 bits, so 255.255.255.100 is not a contiguous mask and is not valid. 248 is 11111000, which gives 5 ones in the last octet, so the prefix is 24 + 5 = /29. There are 3 host bits, so the block has 2^3 = 8 addresses.