Sub topic 2
Anatomy of an IPv6 address
This chapter assumes you know dotted-decimal IPv4 addresses, can convert between binary and hexadecimal, and understand CIDR prefixes. It covers how an IPv6 address is built. Shortened notation is covered in the next chapter.
128 bits in eight groups
An IPv6 address is a 128-bit number. IPv4 uses 32 bits. Each extra bit doubles the number of possible addresses, so IPv6 has 2^128 addresses, about 3.4 × 10^38.
Writing 128 bits in binary or decimal would be unreadable. IPv6 uses hexadecimal and splits the address into eight groups. Each group holds 16 bits and is written as four hex digits. Colons separate the groups.
2001:0db8:abcd:0012:0000:0000:0000:0001The arithmetic is 8 groups × 16 bits = 128 bits. Each hex digit is 4 bits, so 4 digits × 4 bits = 16 bits per group. This form, with all digits written, is called the full or exploded form.
Comparison with IPv4
| Property | IPv4 | IPv6 |
|---|---|---|
| Address length | 32 bits | 128 bits |
| Units | 4 octets of 8 bits | 8 groups of 16 bits |
| Base | Decimal | Hexadecimal |
| Separator | Dot | Colon |
| Total addresses | About 4.3 × 10^9 | About 3.4 × 10^38 |
Network and interface parts
An IPv6 address has two logical parts. The leading bits identify the network, and the rest identify the interface on that network. A typical subnet uses the first 64 bits as the network prefix and the last 64 bits as the interface identifier. In the example above, 2001:0db8:abcd:0012 is the prefix and 0000:0000:0000:0001 is the interface identifier. The Prefixes chapter explains how the boundary is expressed.
Looking at the groups in Python
The ipaddress module reports the length and exposes the full form.
import ipaddress
addr = ipaddress.ip_address("2001:db8:abcd:12:0:0:0:1")
print(addr.version, addr.max_prefixlen) # 6 128
groups = addr.exploded.split(":")
print(len(groups)) # 8
for g in groups:
print(g, int(g, 16))The output is 6 128, then 8, then one line per group, from 2001 8193 through 0001 1. Each group is a number from 0 to 65,535 (ffff).
Common mistakes
- Hex digits are case-insensitive, so
ABCDandabcdare the same. Lowercase is the recommended style. - A group is 16 bits, not 8. Reading a group as a single octet gives wrong prefix boundaries.
- Groups hold up to four digits.
12345is not a valid group.
Exercise
How many bits does the address 2001:0db8:0000:0000:0000:ff00:0042:8329 contain, and how many hex digits does the group "ff00" have? What is the value of the group "0042" in decimal?
Show answer
The address contains 128 bits (8 groups × 16 bits). The group "ff00" has 4 hex digits, which is 16 bits. The group "0042" is 0x42 = 4 × 16 + 2 = 66 in decimal.