Learn

IPv6 Compression Rules

The primary reason why the internet world moved from IPv4 to IPv6 was to deal with the looming exhaustion of the relatively small 32-bit IPv4 address space. By increasing IP addresses from 32 to 128 bits, we ensured that we will probably never again run out of IP addresses. These larger addresses also brought with them flexibility that has enabled other important features, such as automatic device autoconfiguration.

That said, there are also some drawbacks to quadrupling the size of IP addresses. One is that while computers have no difficulty handling 128-bit numbers, humans tend to find them awkward and confusing to read and work with.

Fortunately, the scientists who first defined IPv6 described a couple of notation techniques to make these large addresses easier to handle. IPv6 compression rules describe several shorthands that can be used to make common IPv6 addresses more understandable. In this article, we’ll look at these rules and explain how they work and how you can use them to your advantage.

Executive summary

The following concepts and techniques will be explored in this article.

Concept Description
Bigger is not always better IPv6 addresses provide many advantages, but they are hard to work with in their default state due to their size. The dotted-decimal notation that made IPv4 addresses comprehensible to humans is no longer suitable in IPv6 due to the larger address size.
Going hex IPv6 addresses are expressed as sets of eight 16-bit words using hexadecimal digits, with each word separated from the ones around it using colons.
IPv6 compression rule #1: Eliminate leading zeroes Leading zeroes can be eliminated from each 16-bit word in an IPv6 address to shorten it (but not trailing zeroes).
IPv6 compression rule #2: Replace one sequence of zeroes with a double-colon In each IPv6 address, one lengthy string of zeroes can be replaced by “::” to represent the zeroes that have been removed.
IPv6 compression rule #3: Don’t overuse rule #2 IPv6 compression rule #2 cannot be used more than once in an address or the result will be ambiguity about the full address the compressed version represents.
IPv6 compression rules are even better for special addresses Most special addresses, such as multicast and loopback addresses, have very long strings of zeroes, making them much clearer when compressed.
IPv6 compression calculators There are online tools to convert IPv6 addresses from long to compressed form and back again, though you probably won’t need them.

Bigger is not always better

If you are reading this article, you are undoubtedly already familiar with IPv4 addresses. They are usually shown in what is called dotted decimal notation: four bytes, each expressed as a decimal (base-10) number and separated by periods, as shown below:

Mapping of a 32-bit IPv4 address into dotted decimal notation

This is simple and pretty easy to understand, but that’s because we’re only dealing with 32 bits. Increase that to 128 bits, and you end up with something like this (made-up) IPv6 address:

More than a bit cumbersome, right? That’s not a fun number for a human to deal with. It’s nearly impossible to remember, and it’s likely that errors will be made transcribing or working with it.

{{banner-13="/design/banners"}}

Going hex

The initial step to solving the problem with IPv6 addresses was to go away from decimal numbers and use hexadecimal (hex) notation. In hex, each set of four bits, which has a value of 0 to 15, is replaced by one base-16 digit, with the letters “A” through “F” acting as single-digit stand-ins for 10 to 15. (The letters can be upper or lower case; this is just a matter of style.)

Hex numbers can be confusing for “normal people,” but techies are used to dealing with them. They also have the advantage that each hex digit is exactly equivalent to four bits, so each byte of eight bits is always two hex digits.

Hexadecimal notation is often encountered when working with MAC addresses, the hardware addresses assigned to network devices. These addresses are 48 bits long and are usually expressed as six groups of two hex digits separated by hyphens, like this:

This same method was adapted for IPv6 addresses to make them more uniform. IPv6 addresses are much longer than MAC addresses, so instead of 8-bit bytes with separators, they are grouped into 16-bit words. These are separated by colons, which have the advantage of taking up less space than hyphens (at least in proportional fonts). This is sometimes called colon hexadecimal (or colon hex) notation.

Using this method, the example made-up IPv6 address above is expressed as:

Mapping of a (made up) 128-bit IPv6 address into colon hexadecimal notation

That doesn’t actually appear much shorter than the original, does it? Perhaps not, but it sets the stage for the compression rules we’ll describe below.

IPv6 compression rule #1: Eliminate leading zeroes

While a computer needs to work with every digit of every number, humans don’t need to. In particular, we are accustomed to dropping leading zeroes: If Jane has 20 apples and gives 12 to Jim, we don’t say Jane has “08” apples left, just 8. Similarly, we don’t need to carry around tons of leading zeroes in the numbers within our IPv6 addresses either. 

Eliminating leading zeroes allows us to immediately reduce the example (fake) IP address above as follows:

Now we’re getting somewhere. 

Note that you can only remove leading zeroes, not trailing zeroes, because doing the latter would actually change the number itself. For example, changing the last word here from “F400” to just “F4” would really be changing it from “F400” to “00F4,” thus altering the underlying IPv6 address—not what we want!

{{banner-9="/design/banners"}}

IPv6 compression rule #2: Replace one sequence of zeroes with a double-colon

We mentioned earlier that the IPv6 address space is very large and provides significant flexibility in how addresses are assigned. In fact, the address space is so large that much of it is simply not needed now and is unused, which results in many IPv6 addresses having long strings of zeroes in them (this is especially the case for “special” addresses, as we will see shortly.) 

Accordingly, while it may seem like we “contrived” our example IPv6 address to have 16 zero bits in a row in the middle, even longer sequences of zeroes are in fact quite typical of real-world IPv6 addresses. A good example of this are the link-local IPv6 addresses used widely in the protocol. Here’s an example of a real one without any leading zeroes removed:

The first half of the address is commonly what you see here, with lots of zeroes. If we compress it using rule #1, we end up with:

Again, that’s better, but do we really need to carry around “0:0:0” everywhere, which isn’t telling us much? We do not! IPv6 compression rule #2 says that a single sequence of zeroes can be replaced by the special notation “::” as follows:

This not only shortens the address but also makes it more clear. Seeing “FE80::” at the start of an address makes it immediately clear that it’s link-local, so an administrator doing setup or troubleshooting can just focus on the important digits in the second half of the address (the ones specific to the device).

Going back to our original example of a made-up IPv6 address, it would become:

IPv6 compression rule #3: Don’t overuse rule #2

It’s important to remember that rule #2 described above can only be used once per IPv6 address. This limitation is necessary because the “::” can replace an arbitrary number of zero words, and it doesn’t indicate how many. If you were to do this twice, you would create an ambiguous notation that could refer to more than one possible full-length address. 

Let’s take an example to show how this could occur. Suppose you were given this address:

It’s definitely nice and compact! The problem is that we’ve eliminated four zero words, and it’s unclear how many of them go with each “::” in the address. All of the following are potential original addresses that could create the compressed version above (for clarity, we’re not eliminating leading zeroes in each word here):

Ambiguity is the enemy of clarity, so make sure only to use “::” once per IPv6 address.

Note that, in practice, real-life IPv6 addresses rarely have more than one string of multiple zero words. In the rare cases when this occurs, either string can be replaced by “::” as desired, though it normally would make the most sense to do this for the first such string.

{{banner-10="/design/banners"}}

IPv6 compression rules are even better for special addresses

We’ve seen how “normal” (non-special) IPv6 addresses can have long strings of zeroes in them, making them well suited to IPv6 address compression. Special addresses have even more zeroes because they are intentionally designed to be simple and easy to remember.

Multicast addresses

One example is the set of special multicast addresses maintained by the Internet Assigned Numbers Authority (IANA). A particularly common one of these is the link-local “all nodes” address that is used to send a multicast message to all devices on the local network:

The potential for compression here is pretty easy to see. We can remove the leading zeroes:

We can also get rid of the big clump of zeroes in the middle:

Nice and simple. 

Loopback address

You may be familiar with the IPv4 address 127.0.0.1, which is called the IPv4 loopback address because it always refers to the device itself where it is used. This address is commonly used for testing and troubleshooting.

The equivalent address in IPv6 is:

You’ve probably gotten the hang of this by now and realize that this can be shortened down to simply:

“All-zeroes” address

As a final fun example, consider the special “all-zeroes” IPv6 address, which may be used by a host before it is configured and for other special purposes (such as to identify the default route in routing protocols). As the name suggests, this address is simply a string of zeroes:

Since it is all zeroes, it can be replaced entirely with a double-colon:

No digits at all! Weirdly enough, “::” is in fact a valid IPv6 address.

IPv6 compression calculators

If you do any significant amount of work with IPv6 addresses, you will quickly get accustomed to using the rules above to shorten IPv6 addresses and to mentally reverse the process when necessary. However, if you want some automated help, there are numerous websites online that will let you enter a full-length address and get a compressed version or vice versa. Here’s an example of an address compression calculator and a matching calculator to expand an address.

{{banner-sre="/design/banners"}}

Summary of key concepts

The roomy 128-bit size of IPv6 addresses solves the problem of limited address space in IPv4 and provides flexibility for address assignment, but the addresses themselves are long and clunky to work with. Fortunately, IPv6 addresses have a lot of zeroes in them, and IPv6 compression rules—including eliminating leading zeroes and replacing a string of zeroes with “::”—can make these addresses easier to read and work with. This is particularly the case for the special addresses widely used by network admins. Learning these simple rules can make working with IPv6 addresses much easier and more pleasant.

What's Next?