Skip to content

Instantly share code, notes, and snippets.

@colmmacc
Created April 2, 2019 05:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colmmacc/aa0013c571ab9deeccbf67670ef1b778 to your computer and use it in GitHub Desktop.
Save colmmacc/aa0013c571ab9deeccbf67670ef1b778 to your computer and use it in GitHub Desktop.

How many ways can we express "::"

Both IPv4 and IPv6 allow for some flexibility in how IP addresses are expressed as strings, but IPv6 really takes it to the max. How many ways can we express the all-zeroes IPv6 address, a.k.a. "::". Let's see.

The obvious

 ::

Running count: 1.

As 8 octets

 0000:0000:0000:0000:0000:0000:0000:0000

is the same address. But each octet can also be expressed as either 0, 00, 000, or 0000. So that means that there are 4^8 ways to express "::" as 8 octets.

Running count: 65,537

As 7, 6, 5, 4, 3, 3, 2 octets

 0000::0000:0000:0000:0000:0000:0000
 0000::0000:0000:0000:0000:0000
 0000::0000:0000:0000:0000

these are also valid. As are ...

 0000:0000::0000:0000:0000:0000:0000
 0000:0000::0000:0000:0000:0000
 0000:0000::0000:0000:0000

we can decrease the number of octets, and move the double colon. So we can add 4^7 * 6, 4^6 * 5, 4^5 * 4, 4^4 * 3, 4^3 * 2, and 4^2 to our list.

Running total: 189,329

As an IPv6-compatible-IPv4 address

We can also express the same address as:

 ::0.0.0.0

But each quad in the IPv4 part can be expressed as 0, 00, or 000. So we have 3^4 new possibilities.

Running total: 189,410

As an IPv6-compatible-IPv4 address with 6, 5, 4, 3, 2 octets

We can still expand the IPv6 part of that previous address. E.g.

  0:0:0:0:0:0:0.0.0.0

Which means we can play the same tricks as before. So we need to add 4^6 * 3^4, 4^5 * 4 * 3^4, 4^4 * 4 * 3^4, 4^3 * 2 * 3^4, 4^2 * 3^4, to our set of expressions.

Running total: 926,834

Result

That's 926,834 ways to express the same IP address! Wow. Canonicalizing IPv6 addresses in search functions is going to be very important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment