Understanding Subnetting and CIDR Notation: A Practical Guide

Larbi OUIYZME
4 min readNov 10, 2023

--

Subnetting is a fundamental concept in networking that involves dividing a larger IP network into smaller, more manageable sub-networks. This process aids in efficient resource utilization, security, and network management. One powerful notation for expressing subnet masks is CIDR (Classless Inter-Domain Routing). In this article, we explore the significance of subnetting, dive into CIDR notation, and introduce a practical JavaScript tool for subnet calculations.

Subnetting: Optimizing IP Address Space

Subnetting allows network administrators to optimize the use of IP address space. By breaking down a larger network into smaller subnets, it becomes easier to manage and allocate IP addresses based on organizational needs. Subnetting enhances security by isolating parts of the network and controlling traffic flow.

For instance, consider the IP address range 192.168.1.0/24. The “/24” in CIDR notation indicates that the first 24 bits are used for the network address, leaving 8 bits for host addresses. This provides 256 addresses (2⁸), with one reserved for the network address and another for the broadcast address. Therefore, the number of usable addresses for hosts is 256–2 = 254.

CIDR Notation: A Flexible Addressing Scheme

CIDR, or Classless Inter-Domain Routing, simplifies IP address management by allowing variable-length subnetting. In traditional IP addressing, classes determined the network and host portions of an IP address. CIDR eliminates these restrictions, allowing networks of any size.

In CIDR notation, the IP address is followed by a forward slash and a number representing the subnet mask length. For example, “192.168.1.0/24” denotes a subnet with a 24-bit mask, while “192.168.1.0/16” indicates a 16-bit mask.

Introducing the JavaScript IPv4 Subnet Calculator

To facilitate understanding and implementation of subnetting, a JavaScript IPv4 Subnet Calculator with CIDR Notation has been developed. This tool, authored by Larbi OUIYZME, provides a simple interface for calculating network addresses, broadcast addresses, and the number of usable addresses for hosts.

Source code :

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript IPv4 Subnet Calculator with CIDR Notation</title>
</head>
<body>

<!-- User input section -->
<div>
<label for="ipAddress">IP Address:</label>
<input type="text" id="ipAddress" placeholder="Enter IP Address">
</div>

<div>
<label for="cidrNotation">CIDR Notation:</label>
<input type="number" id="cidrNotation" placeholder="Enter CIDR Notation (1-32)">
</div>

<button onclick="calculateSubnet()">Calculate</button>

<!-- Results section -->
<div>
<label for="networkAddress">Network Address:</label>
<span id="networkAddress"></span>
</div>

<div>
<label for="broadcastAddress">Broadcast Address:</label>
<span id="broadcastAddress"></span>
</div>

<div>
<label for="numberOfAddresses">Number of Usable Addresses for Hosts:</label>
<span id="numberOfAddresses"></span>
</div>

<!-- JavaScript code -->
<script>
// Author: Larbi OUIYZME
// Version: 1.0

// Function to calculate subnet
function calculateSubnet() {
// Get user input
let ipAddress = document.getElementById('ipAddress').value;
let cidrNotation = parseInt(document.getElementById('cidrNotation').value);

// Validate CIDR notation
if (cidrNotation < 1 || cidrNotation > 32) {
alert('CIDR notation must be between 1 and 32.');
return;
}

// Calculate subnet
let { networkAddress, broadcastAddress, numberOfAddresses } = performCalculations(ipAddress, cidrNotation);

// Display results
document.getElementById('networkAddress').innerText = formatIPAddress(networkAddress);
document.getElementById('broadcastAddress').innerText = formatIPAddress(broadcastAddress);
document.getElementById('numberOfAddresses').innerText = numberOfAddresses;
}

// Function to perform subnet calculations
function performCalculations(ip, cidr) {
let ipArray = ip.split('.').map(Number);
let subnetMask = cidrToSubnetMask(cidr);
let networkAddress = applySubnetMask(ipArray, subnetMask);
let numberOfAddresses = Math.pow(2, 32 - cidr) - 2; // Subtracting network and broadcast addresses
let broadcastAddress = calculateBroadcastAddress(networkAddress, subnetMask);

return { networkAddress, broadcastAddress, numberOfAddresses };
}

// Function to convert CIDR to subnet mask
function cidrToSubnetMask(cidr) {
let subnetMask = [0, 0, 0, 0];
for (let i = 0; i < cidr; i++) {
subnetMask[Math.floor(i / 8)] += 1 << (7 - (i % 8));
}
return subnetMask;
}

// Function to apply subnet mask to IP address
function applySubnetMask(ip, subnetMask) {
return ip.map((octet, index) => octet & subnetMask[index]);
}

// Function to calculate broadcast address
function calculateBroadcastAddress(networkAddress, subnetMask) {
let invertedSubnetMask = subnetMask.map(bit => 255 - bit);
let broadcastAddress = networkAddress.map((octet, index) => octet | invertedSubnetMask[index]);
return broadcastAddress;
}

// Function to format IP address
function formatIPAddress(ip) {
return ip.join('.');
}
</script>
</body>
</html>

How to Use the Calculator

  1. Enter the IP address in the “IP Address” field.
  2. Input the CIDR notation (1–32) in the “CIDR Notation” field.
  3. Click the “Calculate” button to perform the subnet calculation.
  4. View the results for the network address, broadcast address, and the number of usable addresses for hosts.

Under the Hood: Modules and Functions

The JavaScript code utilizes several functions and modules to perform subnet calculations:

  • calculateSubnet(): Initiates the subnet calculation process.
  • performCalculations(): Performs the actual subnet calculations based on user input.
  • cidrToSubnetMask(): Converts CIDR notation to a subnet mask.
  • applySubnetMask(): Applies the subnet mask to the IP address.
  • calculateBroadcastAddress(): Determines the broadcast address.
  • formatIPAddress(): Formats the IP address for display.

Explore and Contribute on GitHub

The source code for the JavaScript IPv4 Subnet Calculator is available on GitHub. Feel free to explore, contribute, and tailor the calculator to your specific requirements. Visit the GitHub repository for the latest updates and enhancements.

Conclusion

Understanding subnetting and CIDR notation is crucial for effective network management. The JavaScript IPv4 Subnet Calculator provides a practical and accessible tool for anyone working with IP addresses and networks. By combining theoretical concepts with a user-friendly interface, this calculator empowers users to master subnetting and CIDR notation in a hands-on way.

Happy subnetting!

--

--

Larbi OUIYZME

I'm Larbi, from Morocco. IT trainer and Chief Information Security Officer (CISO), I'm committed to share knowledge. Also, Ham Radio CN8FF passionate about RF