IPv6 prefix and prefix length: calculating the prefix step by step
This lesson is open to everyone. The other lessons open with a package: sign in or sign up, then choose a package.
Key points
- IPv6 has no subnet mask, it has a prefix length:
2001:DB8:AC10:FE01::/64. The number after/is the number of prefix bits. - 1 hex character = 4 bits, 1 group = 16 bits. /64 = the first 16 hex characters (four groups) are the prefix, the last 64 bits the interface ID.
- Writing the prefix: set the host bits to zero, complete the address to 128 bits and shorten it:
2001:DB8:AC10:FE01:0:0:0:0/64→2001:DB8:AC10:FE01::/64. - With a shortened address, first expand
:::2001:DB8::7:AB12:34CD:56EF:7890→2001:0DB8:0000:0007:...→ prefix2001:DB8:0:7::/64. - If the prefix length is not a multiple of 16 or 4, convert the boundary group to binary:
...:9ABC:.../53→ keep the first 5 bits →2001:DB8:CAFE:9800::/53.
A prefix length instead of a subnet mask
IPv4 addresses have a subnet mask. In IPv6, instead of typing something like 255.255.255.0, we use a prefix length. An example of an IPv6 prefix:
2001:DB8:AC10:FE01::/64
This is very similar to writing 192.168.10.0 /24 in IPv4. The number after / is the number of bits used for the prefix. In this example 2001:DB8:AC10:FE01 is the prefix (64 bits), and everything after it can be used for hosts.
In IPv4 we find the network address with the subnet mask. In IPv6 we can also calculate the prefix for any address, it just works a bit differently.
Example 1: /64
An IPv6 address that could be assigned to a host:
2001:DB8:AC10:FE01:21A:2BFF:FE3C:4D5E/64
Which part of this address is the prefix, and which part identifies the host?
/64 means the first 64 bits are the prefix. Since each hex character is 4 bits:
- 64 / 4 = 16 hex characters, which is the first four groups:
2001:0DB8:AC10:FE01. - The rest identifies the host (the interface ID):
021A:2BFF:FE3C:4D5E.
But simply writing the prefix as 2001:DB8:AC10:FE01 is not correct. To write the prefix correctly, we add zeros at the end to complete the address to 128 bits again and add the prefix length:
2001:0DB8:AC10:FE01:0000:0000:0000:0000/64This is a valid prefix, but we can shorten it: the zero groups at the end are replaced by :::
2001:DB8:AC10:FE01::/64
This is the shortest way to write the prefix.
Example 2: a shortened address
2001:DB8::7:AB12:34CD:56EF:7890/64
Before we can see the prefix, we must write the address in full, because it has been shortened (there is a ::). Seven groups are visible, so :: replaces one zero group. We add the zeros to complete 128 bits:
2001:0DB8:0000:0007:AB12:34CD:56EF:7890/64The prefix length is still 64 bits, so the first 16 hex characters are the prefix:
2001:0DB8:0000:0007We add zeros at the end and the prefix length:
2001:0DB8:0000:0007::/64That looks fine, but we can shorten it a little more: drop the leading zeros and 0000 → 0:
2001:DB8:0:7::/64
This is the shortest form of the prefix. Note: we can't replace 0:7 with ::, because :: is already used at the end and may appear only once.
When the prefix length is not a multiple of 16
Depending on the prefix length, the calculation is very easy or a bit harder. In the examples so far the prefix length was 64. What about /53?
| Prefix length | Where the boundary is | Difficulty |
|---|---|---|
| Multiple of 16 (/16, /32, /48, /64) | Between groups | Easy |
| Multiple of 4 (/52, /56, /60) | Between hex characters | Medium |
| Other (/53, /61 ...) | Inside a hex character | Binary needed |
Example 3: /53
2001:DB8:CAFE:9ABC:1111:2222:3333:4444/53
We want to find the prefix of this address. Where do we start?
Step 1: which group contains bit 53? Each group is 16 bits:
| Group | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Bits | 1–16 | 17–32 | 33–48 | 49–64 | 65–80 | 81–96 | 97–112 | 113–128 |
| Value | 2001 | 0DB8 | CAFE | 9ABC | 1111 | 2222 | 3333 | 4444 |
Bit 53 is in the 4th group (bits 49–64). The first three groups (48 bits) are fully in the prefix; from the 4th group, 53 − 48 = 5 bits stay in the prefix.
Step 2: convert that group to binary.
| Hex | 9 | A | B | C |
|---|---|---|---|---|
| Binary | 1001 | 1010 | 1011 | 1100 |
Step 3: keep the first 5 bits, set the remaining host bits to zero.
Before: 10011 | 01010111100 After: 10011 | 00000000000 prefix host (zeroed)
Step 4: convert back to hex.
| Binary | 1001 | 1000 | 0000 | 0000 |
|---|---|---|---|---|
| Hex | 9 | 8 | 0 | 0 |
Step 5: put the group back and set all following host bits to zero.
2001:0DB8:CAFE:9800:0000:0000:0000:0000/53We found the prefix: 2001:DB8:CAFE:9800::/53. It's not hard to calculate, you just have to work with some binary.
Getting used to IPv6 addressing and finding prefixes takes some time, but the more you practise, the easier it becomes.
Check yourself
5 questions · answers are shown instantly · 3 correct answers complete the lesson
1What is the prefix of 2001:DB8:AC10:FE01:21A:2BFF:FE3C:4D5E/64?
2How many hex characters is a /64 prefix?
3What is the prefix of 2001:DB8::7:AB12:34CD:56EF:7890/64?
4With a /53 prefix, which group contains bit 53?
5What is the prefix for 2001:DB8:CAFE:9ABC::1/53?
Comments
Sign in to comment.