Prefix arithmetic and nibble boundaries
Calculate the number of subnets, the first and last address, and the containing prefix for any IPv6 prefix. Find the k-th subnet of a given length inside a larger prefix, working directly in hexadecimal. Explain why nibble-aligned (multiple-of-4) prefix lengths simplify planning, documentation and reverse DNS. Aggregate adjacent prefixes, find the smallest covering prefix, and split a prefix into equal parts.
Take the quiz · pass mark 70% · 10 questions
Explanation
Level 1 showed you how to read an address and its prefix. Address planning needs more: you must be able to answer "how many?", "which one?" and "does it fit?" quickly and without mistakes. Everything in this lesson follows from one idea: a prefix of length n fixes the first n bits and leaves the other 128 − n bits free (RFC 4291, section 2.3).
The three formulas you need
For a prefix of length n:
| Question | Formula | Example for a /48 |
|---|---|---|
| How many addresses? | 2^(128 − n) | 2^80 |
| How many subnets of length m (m ≥ n)? | 2^(m − n) | /64s: 2^16 = 65,536 |
| What is the last address? | first address + 2^(128 − n) − 1 | first address with the last 80 bits all set to 1 |
The first address of a prefix is the address with all free bits set to 0. This is the value written in prefix notation. The last address has all free bits set to 1. Unlike IPv4, IPv6 has no broadcast address, so the last address is not special in itself. (The all-zero interface identifier is the Subnet-Router anycast address on a /64, which matters in L2.05.)
Nibbles: why multiples of 4 are easy
Each hex digit is exactly 4 bits, a nibble. When a prefix length is a multiple of 4, the boundary between "fixed" and "free" falls exactly between two hex digits. You can then read the prefix by counting digits instead of converting to binary:
| Prefix length | Fixed hex digits | Free hex digits | Example (fixed part in bold) |
|---|---|---|---|
| /32 | 8 | 24 | 2001:0db8:… |
| /36 | 9 | 23 | 2001:0db8:4… |
| /40 | 10 | 22 | 2001:0db8:4a… |
| /44 | 11 | 21 | 2001:0db8:4a0… |
| /48 | 12 | 20 | 2001:0db8:4a00:… |
| /52 | 13 | 19 | 2001:0db8:4a00:1… |
| /56 | 14 | 18 | 2001:0db8:4a00:16… |
| /60 | 15 | 17 | 2001:0db8:4a00:16a… |
| /64 | 16 | 16 | 2001:0db8:4a00:16a0:… |
Moving one nibble to the right always multiplies the number of subnets by 16. A /48 holds 16 /52s, 256 /56s, 4,096 /60s and 65,536 /64s.
Nibble alignment matters for more than mental arithmetic:
- Reverse DNS (L2.07) delegates on nibble boundaries. A /56 is exactly one
ip6.arpazone. A /58 is not, and needs workarounds. - Documentation and IPAM read more easily. Everyone can see that
2001:db8:4a00:1600::/56and2001:db8:4a00:1700::/56are neighbours. - Human error drops. A non-nibble prefix such as
2001:db8:4a00:1800::/53covers1800to1fffin the fourth group. It is easy to get wrong when you type in an ACL at 3 a.m.
Non-nibble lengths are still valid and sometimes necessary (a /127 on a point-to-point link, or a /50 to split a /48 into four parts). Use them on purpose, not by accident.
Finding the k-th subnet
The subnets of length m inside a prefix of length n are numbered 0, 1, 2, … 2^(m − n) − 1. The subnet with index k starts at:
first address of the parent + k × 2^(128 − m)
When m is nibble-aligned, this means: write k in hex and place it in the digits between position n and position m. For a /56 inside a /48, the subnet index occupies hex digits 13 and 14 (the first two digits of the fourth group). Index 0x16 (decimal 22) is therefore 2001:db8:4a00:1600::/56.
Be careful with off-by-one errors. "The 23rd /56" is index 22 if you count from 1. This course always says "index" and counts from 0 when precision matters.
Is an address inside a prefix?
An address is inside P/n when its first n bits equal the first n bits of P. For nibble-aligned n, compare the first n/4 hex digits after expanding both. For other lengths, compare whole digits up to the last full nibble, then the remaining 1 to 3 bits of the next digit in binary.
Aggregation and the covering prefix
Two prefixes of the same length /m can merge into a single /(m − 1) only if they are neighbours and the first one has an even index (its last fixed bit is 0). For example:
2001:db8:4a00:1600::/56(index 0x16, even) and2001:db8:4a00:1700::/56aggregate to2001:db8:4a00:1600::/55.2001:db8:4a00:1700::/56and2001:db8:4a00:1800::/56are neighbours, but 0x17 is odd. They do not form a /55. The smallest prefix that covers both is much larger, because0x17 = 0001 0111and0x18 = 0001 1000only share their first four bits.
The smallest covering prefix of a set of prefixes is the longest prefix whose bits all of them share. It is what you would advertise to summarise them, and it often includes space you did not intend to cover. Always check the covering prefix before you write a summary route or a filter.
Splitting into equal parts
To split a prefix into 2^b equal parts, add b bits to its length. Four parts need 2 extra bits, so a /48 becomes four /50s. Because /50 is not nibble-aligned, the four starts are 0000, 4000, 8000 and c000 in the fourth group. Each quarter steps by 0x4000.
Drawing diagram…
Writing results correctly
Write every result in RFC 5952 canonical form (L1.02): lower-case hex, no leading zeros, and :: for the longest run of two or more zero groups. In prefix notation, the address part must be the first address of the prefix. 2001:db8:4a00:1234::/56 is not a valid way to name a prefix, because it has host bits set. The correct name is 2001:db8:4a00:1200::/56.
Worked examples
All examples use the documentation prefix 2001:db8:4a00::/48.
Example 1: Counting
How many /56, /60 and /64 subnets does 2001:db8:4a00::/48 hold?
- /56: 2^(56 − 48) = 2^8 = 256
- /60: 2^(60 − 48) = 2^12 = 4,096
- /64: 2^(64 − 48) = 2^16 = 65,536
Example 2: The k-th subnet
Find the /56 with index 22 inside 2001:db8:4a00::/48.
22 in hex is 0x16. A /56 inside a /48 uses hex digits 13 and 14, which are the first two digits of the fourth group. Result: 2001:db8:4a00:1600::/56.
Example 3: First and last address
What are the first and last addresses of 2001:db8:4a00:1600::/56?
The first 14 hex digits are fixed. The first address sets the rest to 0: 2001:db8:4a00:1600::. The last address sets the rest to f: 2001:db8:4a00:16ff:ffff:ffff:ffff:ffff.
Example 4: Membership on a non-nibble boundary
Is 2001:db8:4a00:17ab::1 inside 2001:db8:4a00:1600::/55?
A /55 fixes the first 13 hex digits plus 3 bits of the 14th digit. The 14th digit is 6 (0110) in the prefix and 7 (0111) in the address. The first three bits (011) match, so the answer is yes. The /55 covers fourth-group values 1600 to 17ff.
Example 5: Aggregation and covering prefix
2001:db8:4a00:1600::/56+2001:db8:4a00:1700::/56→2001:db8:4a00:1600::/55(0x16 is even).2001:db8:4a00:1700::/56+2001:db8:4a00:1800::/56→ no /55. The smallest covering prefix is2001:db8:4a00:1000::/52, which covers 16 /56s, not 2.
Example 6: Split into four
Split 2001:db8:4a00::/48 into four equal parts: 2001:db8:4a00::/50, 2001:db8:4a00:4000::/50, 2001:db8:4a00:8000::/50, 2001:db8:4a00:c000::/50.
Example 7: Ranges in a plan
A hosting provider gives each Proxmox cluster a /52 from 2001:db8:4a00::/48. How many clusters fit, and which /52 is cluster index 10? 2^(52 − 48) = 16 clusters. Index 10 is 0xa, the first digit of the fourth group: 2001:db8:4a00:a000::/52.
Lab
Goal: Become fast and accurate at prefix arithmetic, then automate it with a script you can reuse in later levels.
You need: Python 3.8 or later. sipcalc is optional (apt install sipcalc on Debian/Ubuntu, brew install sipcalc on macOS).
Step 1: Explore with the tools
python3 - <<'PY'
import ipaddress as ip
p = ip.IPv6Network("2001:db8:4a00::/48")
print(p.num_addresses, 2**80)
s56 = list(p.subnets(new_prefix=56))
print(len(s56), s56[22], s56[22][-1])
print(ip.IPv6Network("2001:db8:4a00:1600::/55").supernet_of(ip.IPv6Network("2001:db8:4a00:17ab::/64")))
print(list(ip.collapse_addresses([ip.IPv6Network("2001:db8:4a00:1600::/56"), ip.IPv6Network("2001:db8:4a00:1700::/56")])))
PY
sipcalc 2001:db8:4a00:1600::/56
Explain each line of output using the formulas above. Note that collapse_addresses only merges prefixes that form an exact aggregate. It never returns a covering prefix that includes extra space.
Step 2: Timed drills
Write drill.py. It picks a random /48 inside 2001:db8::/32 and asks ten questions of these four kinds: count the subnets of length m, give the k-th subnet, give the last address, and say whether an address is inside a prefix. Use ipaddress to check the answers and accept any spelling that normalises to the same value. Record your time. Aim for ten correct answers in under five minutes, using no tools for the nibble-aligned cases.
Step 3: The plan splitter
Write split48.py. It takes a /48 and a small YAML or CSV file of named blocks, and prints a table. Example input:
infrastructure,56,0
servers,56,1
users-hq,56,16
users-branch,56,32
lab,60,0x300
Each line is name, prefix length, and index within the /48. The script prints the prefix, its first and last address, and the number of /64s it holds. It must refuse overlapping blocks. Extend it to print, for each /56, its /64s numbered by VLAN (for example VLAN 10 is index 0x10 in that /56).
What to submit
Your drill score and time, split48.py, and its output for 2001:db8:4a00::/48 with the example input. Also include one overlap case that your script correctly rejects.
Summary
- A /n fixes n bits. It holds 2^(128 − n) addresses and 2^(m − n) subnets of length m. The last address is the first + 2^(128 − n) − 1.
- On nibble-aligned lengths (multiples of 4) the boundary is a hex digit. Count digits instead of converting to binary, and remember that each nibble multiplies the subnet count by 16.
- The subnet with index k starts at parent + k × 2^(128 − m). For nibble-aligned m, write k in hex in the right digits.
- Two equal-length prefixes aggregate only if they are neighbours and the first has an even index. Otherwise the covering prefix is larger and includes extra space.
- Splitting into 2^b parts adds b bits. Prefix names must have no host bits set and should be written in RFC 5952 form.
- Next lesson (L2.02): where the big prefixes come from in the first place.
Quiz imported · 10 questions · pass mark 70%. Lab questions are not auto-marked.