Image for post
Image for post

Converting CIDR Block to IP Range in Your Head in 9 Seconds

I frequently come across people that use online CIDR tools whenever they need to calculate an IP range for a CIDR block. Little do these people know that you can actually calculate an IP range in your head in just about 9 seconds!

Read on to learn the 2 key steps that make this possible!

Getting the First and the Last IP Addresses

First some background:

Let’s take a look at a random CIDR block:

3.58.1.97/12

Notation consists of the IP address and the prefix number. Prefix number is the amount of locked bits from the left side. IP address is the start of the IP address range covered by the CIDR block.

Bits? What bits?

Every IP address can be represented in binary form. For example the IP address in our CIDR block, when rewritten in binary form, looks like:

00000011.00111010.00000001.01100001

That’s, by the way, why every section of an IPv4 is also known as “octet” (meaning the group of 8). Locked bits in our IP address are:

00000011.00111010.00000001.01100001

What’s the quickest and the most memorable way to calculate the first and the last IP addresses of the IP range?

Concept of balance

What’s the amount of bits that can change in our address? Total amount of bits in the binary representation of an IPv4 is 32. As stated in our CIDR block amount of locked bits is 12. That subtracted from 32 leaves us with 20 bits.

Think of these 20 bits as the balance that you can “spend” on octets in an IP address. You always start spending from the right side. An example:

Total balance: 20 bits.Spend 8 bits.
00000011.00111010.00000001.01100001
Spend 8 more bits.
00000011.00111010.00000001.01100001
Current balance: 4 bits.
Number of full octets that were covered: 2.
Remaining Balance: 4 bits.Spend the remaining 4 bits.
00000011.00111010.00000001.01100001

Knowing how many full octets we have covered is particularly helpful since it allows us to quickly calculate the respective parts of the first and the last IP addresses.

A fully covered octet is a fully unlocked octet. Numbers in such octet span the entire value range from 8 zeros to 8 ones or in decimal form from 0 to 255.

For each 8 in our bits balance we get a fully unlocked octet from the right side.

Here’s what we know about the first and the last IP addresses so far:

First IPv4   : XXX.XXX.0.0
Last IPv4 : XXX.XXX.255.255

The tricky part

How do we calculate the values for the third octet from the right?

One thing we know right away is that the value of the third octet in the first IP address will be equal to the value of the third octet of the IP address specified in our CIDR block:

First IPv4   : XXX.58.0.0
Last IPv4 : XXX.XXX.255.255

That’s because an IP address specified in the CIDR block is the start of the IP address range covered by a CIDR block.

To calculate the value of the same octet for the last IP address, take 2 to the power of the amount of bits you spent on the octet, and add that to the value of the same octet in the first IP address.

4 bits spent. 2 ^ 4 = 16.
16 is the range of change of the 3rd octet.
First IPv4 : XXX.58.0.0
Last IPv4 : XXX.73.255.255 (!?)

Do you know why it’s not 74? Isn’t the 58 + 16 = 74? That is correct, but the remaining 65536 addresses of the last 16th subnet of type B in our range are already covered by the two last octets:

First IPv4   : XXX.58.0.0
Last IPv4 : XXX.73.255.255

Finally for the fourth octet from the right we simply copy the value of the same octet from the IP address specified in the CIDR block. Values of locked octets remain the same throughout all IP addresses covered by the CIDR:

First IPv4   : 3.58.0.0
Last IPv4 : 3.73.255.255

That’s about it!

Summarizing

Just remember the three things to do to calculate the first and last IP addresses of the CIDR:

  • Find fully unlocked octets. For these octets use 0 and 255 for the first and the last IP addresses respectively.
  • Calculate the range of change in the partially unlocked octet. Don’t forget that the last subnet is covered by preceding octets (from the right).
  • For locked octets use values from the same octets in the IP address specified in the CIDR block.

That’s all you need to quickly figure out the first and the last IP addresses of a CIDR block!

Tip: memorizing the table of powers of 2 will greatly speed up the second step!

Practice time!

Up for a challenge? Try calculating the following CIDR block ranges!

10.8.17.0/13
11.5.0.0/8
172.16.32.0/22
19.8.8.77/13
255.255.255.253/11 (hardcore)
0.0.0.0/19

Leave your answers in the comments below! I promise to review every submission :)

If you found this article valuable please share it! And thank you for reading!

Written by

Writing full-stack React.js applications and building cloud solutions on AWS. Find me on GitHub and LinkedIn @arkadyt or at www.arkadyt.dev

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store