CIDR, which stands for Classless Inter-Domain Routing, is a method for allocating IP addresses and IP routing. It was introduced by the Internet Engineering Task Force in 1993 to replace the previous addressing architecture of Classful network design (Class A, B, and C networks).
How the CIDR Subnet Calculator Works
A subnet calculator takes an IP address and a netmask (CIDR prefix) and calculates the resulting network properties. This is essential for network administrators to determine how many devices can reside on a specific network and what the network boundaries are.
Network Address: The first address in the range, identifying the network itself.
Broadcast Address: The last address in the range, used to communicate with all hosts on the network.
Usable Host Range: The IP addresses that can be assigned to devices like computers, printers, and routers.
Subnet Mask: The dotted-decimal representation of the CIDR prefix (e.g., /24 is 255.255.255.0).
Common CIDR Examples
Here are some of the most common CIDR prefixes used in local and wide area networks:
CIDR
Subnet Mask
Total Hosts
/30
255.255.255.252
4
/24
255.255.255.0
256
/16
255.255.0.0
65,536
/8
255.0.0.0
16,777,216
Usable vs Total Hosts
In most standard IPv4 networking, the first address (Network) and the last address (Broadcast) cannot be assigned to hosts. Therefore, the number of usable hosts is typically (2 ^ host_bits) - 2. For example, in a /24 network, there are 256 total addresses, but only 254 are usable for actual devices.
function calculateSubnet() {
var ipStr = document.getElementById("ipAddress").value.trim();
var cidr = parseInt(document.getElementById("cidrMask").value);
var errorDiv = document.getElementById("errorMessage");
var resultsDiv = document.getElementById("results");
errorDiv.style.display = "none";
resultsDiv.style.display = "none";
// Validate IP Address format
var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var match = ipStr.match(ipPattern);
if (!match) {
errorDiv.textContent = "Error: Invalid IP Address format.";
errorDiv.style.display = "block";
return;
}
var ipParts = [];
var ipInt = 0;
for (var i = 1; i <= 4; i++) {
var part = parseInt(match[i]);
if (part 255) {
errorDiv.textContent = "Error: IP segments must be between 0 and 255.";
errorDiv.style.display = "block";
return;
}
ipParts.push(part);
ipInt = (ipInt <>> 0;
// Calculate Mask
var maskInt = 0;
if (cidr > 0) {
maskInt = (0xFFFFFFFF <>> 0;
}
var wildcardInt = (~maskInt) >>> 0;
var networkInt = (ipInt & maskInt) >>> 0;
var broadcastInt = (networkInt | wildcardInt) >>> 0;
// Host calculations
var totalHosts = Math.pow(2, (32 – cidr));
var usableHosts = (cidr <= 30) ? totalHosts – 2 : (cidr === 31 ? 2 : 1);
var firstHostInt = (cidr <= 30) ? networkInt + 1 : networkInt;
var lastHostInt = (cidr >> 24) & 0xFF) + "." +
((int >>> 16) & 0xFF) + "." +
((int >>> 8) & 0xFF) + "." +
(int & 0xFF);
}
document.getElementById("resNetwork").textContent = intToIp(networkInt);
document.getElementById("resBroadcast").textContent = intToIp(broadcastInt);
document.getElementById("resMask").textContent = intToIp(maskInt);
document.getElementById("resWildcard").textContent = intToIp(wildcardInt);
document.getElementById("resFirst").textContent = intToIp(firstHostInt);
document.getElementById("resLast").textContent = intToIp(lastHostInt);
document.getElementById("resTotal").textContent = totalHosts.toLocaleString();
document.getElementById("resUsable").textContent = usableHosts < 0 ? 0 : usableHosts.toLocaleString();
document.getElementById("resCidr").textContent = ipStr + " /" + cidr;
resultsDiv.style.display = "block";
}