What are the two rules for compressing (shortening) IPv6 addresses?
Rule 1: omit leading zeros in each hextet. Rule 2: replace ONE contiguous run of all-zero hextets with :: (usable only once per address).
The two rules just remove zeros that the reader can unambiguously put back, so a full 128-bit address stays writable by hand. Rule 1 drops only leading zeros inside each 4-digit hextet (0db8 → db8); trailing zeros must stay, because db80 and 0db8 are different values. Rule 2 then collapses a whole run of all-zero hextets into a single ::. The reason :: may appear only once is that expansion works by counting the hextets present and padding the gap — two gaps would be impossible to size unambiguously, so an address like 2001::1::2 is illegal.
* The two compression rules applied in order, and why a second :: is illegal. *
IPv6 Address Compression Rules:
Rule 1: Omit Leading Zeros
- Remove leading zeros within each hextet
- Trailing zeros must remain
| Original | Compressed |
|---|---|
| 01ab | 1ab |
| 00ab | ab |
| 000a | a |
| 0000 | 0 |
Rule 2: Double Colon (::)
- Replace ONE sequence of consecutive all-zero hextets with ::
- Can only be used ONCE per address (prevents ambiguity)
Example:
Full: 2001:0db8:0000:0000:0000:0000:0000:0001
Rule 1: 2001:db8:0:0:0:0:0:1
Rule 2: 2001:db8::1
Warning: Using :: twice is INVALID:
2001::1::2- Invalid! Can't determine how many zeros each :: represents
Go deeper:
RFC 4291 §2.2 — Text Representation of Addresses — the rule that
::indicates one run of zero hextets and may appear only once.IPv6 address (Wikipedia) — leading-zero suppression and
::collapsing, with the canonical-form preference for the longest run.