IP addresses are used throughout the Internet and in private networks to represent individual devices, such as laptops, smartphones, printers, servers, and routers. In order to participate in a network, a device must have a unique IP address. Computer networks can be viewed as collections of hosts, each with a unique IP address, that can communicate with each other.
Networks of IP addresses can be represented in different forms. In this article, I will clarify the CIDR notation for IP addresses and show how you can convert between IP ranges and CIDR block notation.
What is CIDR Notation?
CIDR notation was developed in the early 1990s as a more flexible way of defining subnets. The previous system for IPv4 networks consisted of classes - class A, B, and C - each a different size and able to support a different number of hosts. The class system for networks is summarized in the table below.
IP Address Class | # Subnet Bits | Subnet Mask | # Addresses | Example IP Address Range |
---|---|---|---|---|
Class A | 8 | 255.0.0.0 | 16,777,216 | 10.0.0.0 - 10.255.255.255 |
Class B | 16 | 255.255.0.0 | 65,534 | 192.168.1.0 - 192.168.1.255 |
Class C | 24 | 255.255.255.0 | 254 | 10.5.1.0 - 10.5.1.255 |
CIDR notation consists of an IP address followed by a forward slash followed by the number of subnet bits. CIDR notation gives a compact format for specifying the size of a subnet. Using CIDR notation, we can define blocks of IP addresses of any size, whereas the class system only allows for three sizes.
How to Convert an IP Range to CIDR Notation
I'll take you through the process of converting IP ranges to CID notations and CIDR notation to IP address ranges. The process is pretty straightforward.
To convert an IP range to CIDR notation, select an IP address from the subnet (ideally the network address) and add a forward slash (/) followed by the number of bits representing the subnet mask (example: 192.168.1.0/24). If you're not familiar with subnets and masks, I have a good article on subnetting that will walk you through the details.
Let's look at an example. Suppose you have the IP address range 192.168.1.0 through 192.168.1.255. The subnet mask for this subnet is 255.255.255.0. If we convert the subnet mask to its binary representation, we get a sequence of 24 ones and 8 zeroes: 11111111111111111111111100000000
. The number of ones is the same as the number of subnet bits, 24. The CIDR notation for the IP range 192.168.1.0 - 192.168.1.255 is 192.168.1.0/24.
Let me summarize what we just did to make it clear. To convert an IP address range to CIDR notation, convert the subnet mask to binary, count the number of ones, and add this number to the IP address following a forward slash. This process can be reversed to obtain the IP range again.
That's all there is to it! The CIDR format is a more compact notation than an IP address range and I find it much easier to read. It is trivial to convert between CIDR notation and IP ranges, especially with the help of an online subnet calculator like the one below.
Online Subnet Calculator
How to Convert CIDR Notation to an IP Range
Let's look at another example to illustrate how we would convert a CIDR block to an IP address range. The example we used above was easy because 24-bit subnet masks fall cleanly on the boundary of an octet (remember, there are 8 bits in a byte and each octet is a byte in length).
In this example, we will start with a CIDR range with a 27-bit subnet mask and convert it to its range of IP addresses. To convert CIDR blocks to IP ranges, do the following:
- Covert the given IP address and subnet mask from decimal to binary (you might find a binary converter helpful).
- Line up the binary IP address and binary subnet mask.
- The first address (network address) in a range of IPs is a bitwise AND of the network portion of the IP address and subnet mask.
- The last address (broadcast address) in a range of IPs is found by setting all bits in the host potion of the IP address to 1.
Step 1: Convert the IP address and subnet mask to binary:
192.168.1.32
➡️ 11000000 10101000 00000001 100000
/27
➡️ 11111111 11111111 11111111 11100000
Step 2: Line up the binary IP address and subnet mask:
11000000 10101000 00000001 00100000
11111111 11111111 11111111 11100000
Step 3: Find the first address (bitwise AND):
11000000 10101000 00000001 001▀▀▀▀▀
11111111 11111111 11111111 111▀▀▀▀▀
-------------------------------------------------------
11111111 11111111 11111111 111▀▀▀▀▀
➡️ 192.168.1.32
Step 4: Find the last address (set all host bits to 1):
11000000 10101000 00000001 00100000
11000000 10101000 00000001 00111111
➡️ 192.168.1.63
The full range of IP addresses in this subnet is 192.168.1.32 - 192.168.1.63. Incrementing the host potion of the address space by one, 192.168.1.64, would put you in the next subnet.
How to Calculate the Number of Hosts
It is useful to know how to calculate the number of hosts in a subnet so you can appropriately size a network. The calculation is quite simple.
To calculate the number of available hosts in a subnet, raise 2 to the power of 32 minus the number of subnet bits and subtract 2. The formula is (2 ^ (32 - n)) - 2, where n is the number of bits in the subnet mask. For example, a /24 subnet has (2 ^ (32 - 24)) - 2 = 254 assignable hosts. A /27 subnet has (2 ^ (32 - 27)) - 2 = 30 assignable hosts.
CIDR Notation in Practice
In this article, you have seen what CIDR notation is and how to calculate different parts of a subnet. CIDR notation is widely used in networking to specify IP address ranges for subnets, since it is a more compact form that concisely conveys the necessary details about the subnet. In practice, CIDR notation is used to create smaller subnets that are more efficient for routing traffic and can provide better security and flexibility for network administrators.
In routing tables, CIDR notation can be used to represent network prefixes. Each entry in a routing table typically includes a destination network address, subnet mask, and next hop to route traffic to the network. Some routing devices allow for specification in CIDR notation, like in the example routing table below:
Destination | Next Hop |
---|---|
10.0.0.0/16 | 192.168.1.1 |
192.168.1.0/24 | 192.168.1.2 |
192.168.2.0/24 | 192.168.1.3 |
You will frequently see CIDR notation used as a shorthand for representing an entire range of IP addresses in network diagrams and other technical documentation. Think of CIDR notation as an essential tool for network administrators to create organized subnets in their networks. Practice the calculations, and I think you will be amazed how quickly the calculations become natural.
Resources for More Information
Below are a few resources for more information about Classless Inter-Domain Routing.
Resource | Publisher |
---|---|
What Is CIDR Notation? | WhatIsMyIPAddress |
What is subnetting? | NetworkCalc |
CIDR Notation - Explained & Examples | ShellHacks |
Using NetworkCalc's free APIs | NetworkCalc |
Subnet Chart - Free PDF Download | NetworkCalc |