CIDR Simplified: Network, Broadcast, and Subnet Mask Calculations

As a DevOps Engineer or a Network Administrator you have to deal with a lot of IP address ranges in CIDR (Classless Inter-Domain Routing) format. It is essential for them to have a skill to calculate the network address, broadcast address, and subnet mask for an IP address with a CIDR range.

In this article I explain how to calculate the IP network parameters manually and provide an automation script in Bash to ease the job of calculation.

💡
Automation script that calculates the IP network parameters from the input IP address in CIDR range format can be found here: https://github.com/Brain2life/bash-cookbook/tree/cidrcalc

Understanding CIDR Notation

CIDR notation is a method for allocating IP addresses and IP routing. It is denoted as IP address/PREFIX, where the PREFIX indicates the number of leading 1-bits in the subnet mask. For example, in the CIDR notation 192.168.1.0/24, the /24 indicates that the first 24 bits of the IP address are the network part.

Calculate the Subnet Mask

The subnet mask can be calculated from the PREFIX part of the CIDR notation. The number after the slash (/) represents the number of bits set to 1 from the left in the subnet mask. For a /24 prefix, the subnet mask is 255.255.255.0 because the first 24 bits are set to 1, and the remaining bits are set to 0.

Calculate the Network Address

The network address is determined by performing a bitwise AND operation between the IP address and the subnet mask. This operation retains the network portion of the IP address while setting the host portion to 0.

Calculate the Broadcast Address

The broadcast address is calculated by performing a bitwise OR operation between the network address and the inverted subnet mask (where the network bits are set to 0 and the host bits are set to 1) . This operation retains the network portion of the IP address while setting the host portion to 1.

Example Calculation

Let's say we have an IP address with a CIDR range of 192.168.1.10/24.

💡
To convert decimal numbers use bdconvert tool. For more information, see How to manually convert decimal numbers to binary and vice versa
  1. Subnet Mask: For a /24 prefix, the subnet mask is 255.255.255.0.

  2. Network Address: Perform a bitwise AND between 192.168.1.10 and 255.255.255.0, resulting in 192.168.1.0.

  3. Broadcast Address: Perform a bitwise OR between network address 192.168.1.0 and the inverted subnet mask 0.0.0.255, resulting in 192.168.1.255.

Alternative approach: Use cross line method

In this method to calculate the network, broadcast address and subnet mask, you need to do the following steps:

  1. Convert IP address to binary and draw a line separating the network and host portion of the IP address according to the CIDR range.

  2. Keep in mind that network address is the smallest possible address that you can have. Therefore, to get the network address, you need to set all bits that are on the right side of the crossing line to "0".

  3. The broadcast address is the largest possible address that you can have. Therefore, to get the broadcast address, you need to set all bits that are on the right side of the crossing line to "1".

  4. Subnet mask shows the portion of the IP address that is responsible for the network and host part. For the network part subnet mask have all "1". For the host part - all "0". To get the subnet mask you need to set all bits on the left side of the crossing line to "1" and on the right side to "0".

  5. Finally convert all bits to decimal numbers and get the required IP subnet attributes.

    Below is an example of calculating the network, broadcast addresses and subnet mask for the network 30.178.248.205/26

To check the calcualtions you can use the cidrcalc tool:

cidrcalc 30.178.248.205/26

References:

  1. https://t.me/devops_orbit

  2. cidrcalc

  3. bdconvert

  4. Subnet Quiz

  5. RapidTables to convert from decimal to binary and vice versa

  6. YouTube: Subnetting Made Simple

  7. How to manually convert decimal numbers to binary and vice versa