What are the rules for shortening an IPv6 address?
I see the same IPv6 address written three different ways in different tools. What compression is legal, and why does 2001:db8::1::2 get rejected?
1 Answer
Two rules, applied in order. First, drop leading zeros in each 16-bit group: 2001:0db8:0000:0042 → 2001:db8:0:42. Second, compress one consecutive run of all-zero groups to :: — the longest run, leftmost on a tie. 2001:db8:0:0:0:0:0:1 → 2001:db8::1. The reason :: may appear only once: it means "fill with zeros," and with two of them the parser can't tell how many zero groups each represents — the address becomes ambiguous, so it's illegal. When expanding, count the groups present, subtract from 8, and that's how many zero groups the :: hides. Practice both directions; exams love giving the expanded form and four compressed candidates that differ by exactly these rules.