How to Calculate a CIDR

Sep 15, 2021

Have you ever been confused by this notation at the end of an IP address?

192.168.1.1/16

This is called the CIDR (Classless Inter-Domain Routing), and it represents a block of IP addresses. In this post, you'll learn how to calculate that range in your head.

In an IP, each block refers to 8 bits (an octet). Usually, we convert each block to decimal to make it more readable, e.g., 192.168.1.1. The CIDR represents a right-padded bitmask that's applied to the IP address.

For example, /8 corresponds to 11111111.00000000.00000000.00000000 and the more readable subnet mask 255.0.0.0.

How many addresses does a CIDR block represent? You calculate 2``32-prefix, where prefix is the number after the slash.

For example, /29 contains 232-29=23=8 addresses. Here's a quick table that you can reference for the most CIDR blocks.

It's easy to calculate how many subnets can fit inside a larger one. For example, how many /30 networks fit in a /27 network?  Using the properties of exponents it's easy. 2``32-27``/2``32-30 = 2``5``/2``2``= 2``5-2 = 2``3 = 8

To calculate the exact range that an IP and CIDR block corresponds to is a bit more challenging to do in your head. For small blocks, it's simple. For larger blocks, it might be tough to do the math in your head (although it's the same!). A host of calculators online can calculate the first and last usable IPs based on the command, like cidr.xyz.

As a caveat, you should remember that each range should reserve an address for the network and broadcast addresses, so the effective number of addresses is two fewer than what you calculated.

Subscribe for email updates on startups, engineering, and AI