CIDR Subnet Calculator
.cidr-calculator-container {
font-family: Arial, sans-serif;
max-width: 600px;
margin: 20px auto;
padding: 20px;
border: 1px solid #ddd;
border-radius: 8px;
background-color: #f9f9f9;
}
.cidr-calculator-container h2 {
text-align: center;
color: #333;
margin-bottom: 20px;
}
.calculator-form .form-group {
margin-bottom: 15px;
}
.calculator-form label {
display: block;
margin-bottom: 5px;
font-weight: bold;
color: #555;
}
.calculator-form input[type="text"],
.calculator-form input[type="number"] {
width: calc(100% – 22px);
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
}
.calculator-form button {
display: block;
width: 100%;
padding: 12px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 18px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.calculator-form button:hover {
background-color: #0056b3;
}
.calculator-result {
margin-top: 25px;
padding: 15px;
border: 1px solid #e0e0e0;
border-radius: 4px;
background-color: #e9ecef;
min-height: 100px;
}
.calculator-result p {
margin: 8px 0;
font-size: 16px;
color: #333;
}
.calculator-result p strong {
color: #000;
}
.error-message {
color: red;
font-weight: bold;
}
function ipToInt(ip) {
var parts = ip.split('.');
return ((parseInt(parts[0], 10) << 24) |
(parseInt(parts[1], 10) << 16) |
(parseInt(parts[2], 10) <>> 0; // Use >>> 0 for unsigned 32-bit integer
}
function intToIp(intVal) {
return ((intVal >>> 24) & 0xFF) + '.' +
((intVal >>> 16) & 0xFF) + '.' +
((intVal >>> 8) & 0xFF) + '.' +
(intVal & 0xFF);
}
function calculateSubnet() {
var ipAddressInput = document.getElementById('ipAddress').value.trim();
var cidrPrefixInput = document.getElementById('cidrPrefix').value.trim();
var resultDiv = document.getElementById('result');
resultDiv.innerHTML = "; // Clear previous results
// Validate IP Address
var ipParts = ipAddressInput.split('.');
if (ipParts.length !== 4) {
resultDiv.innerHTML = 'Invalid IP Address format. Must be four octets separated by dots.';
return;
}
for (var i = 0; i < ipParts.length; i++) {
var octet = parseInt(ipParts[i], 10);
if (isNaN(octet) || octet 255) {
resultDiv.innerHTML = 'Invalid IP Address. Each octet must be between 0 and 255.';
return;
}
}
// Validate CIDR Prefix Length
var prefixLength = parseInt(cidrPrefixInput, 10);
if (isNaN(prefixLength) || prefixLength 32) {
resultDiv.innerHTML = 'Invalid CIDR Prefix Length. Must be a number between 0 and 32.';
return;
}
var ipInt = ipToInt(ipAddressInput);
// Calculate Subnet Mask
var subnetMaskInt = (0xFFFFFFFF <>> 0;
var subnetMask = intToIp(subnetMaskInt);
// Calculate Network Address
var networkAddressInt = (ipInt & subnetMaskInt) >>> 0;
var networkAddress = intToIp(networkAddressInt);
// Calculate Broadcast Address
var broadcastAddressInt = (networkAddressInt | (~subnetMaskInt >>> 0)) >>> 0;
var broadcastAddress = intToIp(broadcastAddressInt);
// Calculate Number of Hosts
var totalHosts = Math.pow(2, (32 – prefixLength));
var usableHosts = 0;
if (prefixLength < 31) { // For /31 and /32, there are no usable hosts
usableHosts = totalHosts – 2;
}
// Calculate First and Last Usable IP
var firstUsableIp = "N/A";
var lastUsableIp = "N/A";
if (prefixLength < 31) { // For /31 and /32, there are no usable hosts
firstUsableIp = intToIp(networkAddressInt + 1);
lastUsableIp = intToIp(broadcastAddressInt – 1);
} else if (prefixLength === 31) {
// For /31, the two addresses are network and broadcast, no usable hosts
firstUsableIp = "N/A";
lastUsableIp = "N/A";
} else if (prefixLength === 32) {
// For /32, the single address is the host itself, no network/broadcast distinction in the traditional sense
firstUsableIp = "N/A";
lastUsableIp = "N/A";
}
resultDiv.innerHTML =
'
IP Address: ' + ipAddressInput + " +
'
CIDR Prefix: /' + prefixLength + " +
'
Subnet Mask: ' + subnetMask + " +
'
Network Address: ' + networkAddress + " +
'
Broadcast Address: ' + broadcastAddress + " +
'
First Usable IP: ' + firstUsableIp + " +
'
Last Usable IP: ' + lastUsableIp + " +
'
Total Hosts: ' + totalHosts + " +
'
Usable Hosts: ' + usableHosts + ";
}
Understanding CIDR and Subnetting
Classless Inter-Domain Routing (CIDR) is a method for allocating IP addresses and routing Internet Protocol packets. It was introduced in 1993 to replace the previous classful network addressing architecture on the Internet, which was inefficient and led to rapid exhaustion of IPv4 address space. CIDR allows for more flexible and efficient use of IP addresses by enabling subnetting, which divides a larger network into smaller, more manageable sub-networks.
What is CIDR?
At its core, CIDR notation combines an IP address with a suffix that indicates the number of bits used for the network portion of the address. For example, 192.168.1.0/24 means that 192.168.1.0 is the network address, and the first 24 bits (from the left) are dedicated to identifying the network, leaving the remaining 8 bits for host addresses within that network.
Key Components of a CIDR Subnet:
- IP Address: The starting point for the calculation, typically an address within the desired network.
- CIDR Prefix Length: The number of bits (from 0 to 32 for IPv4) that define the network portion of the IP address. A smaller number means a larger network (more hosts), while a larger number means a smaller network (fewer hosts).
- Subnet Mask: A 32-bit number that masks an IP address to delineate the network address from the host address. It's derived directly from the CIDR prefix length. For a /24 prefix, the subnet mask is 255.255.255.0. All bits corresponding to the network portion are set to 1, and all bits corresponding to the host portion are set to 0.
- Network Address: The first IP address in a given subnet. It's identified by setting all host bits to 0. This address is reserved for identifying the network itself and cannot be assigned to a host.
- Broadcast Address: The last IP address in a given subnet. It's identified by setting all host bits to 1. Packets sent to this address are delivered to all hosts within that subnet. This address is also reserved and cannot be assigned to a host.
- First Usable IP: The first IP address in the subnet that can be assigned to a host device. This is always the network address plus one.
- Last Usable IP: The last IP address in the subnet that can be assigned to a host device. This is always the broadcast address minus one.
- Total Hosts: The total number of IP addresses available within the subnet, including the network and broadcast addresses. Calculated as
2^(32 - prefix_length).
- Usable Hosts: The number of IP addresses available for assignment to actual devices (computers, servers, routers, etc.) within the subnet. This is typically
Total Hosts - 2 (subtracting the network and broadcast addresses). For /31 and /32 subnets, there are no usable hosts in the traditional sense.
How Subnetting Works
Subnetting involves borrowing bits from the host portion of an IP address to create additional sub-networks. This allows organizations to segment their networks for better security, performance, and management. For example, a company might have a main network 192.168.0.0/16. They could then create smaller subnets for different departments, like 192.168.1.0/24 for Sales, 192.168.2.0/24 for Marketing, and so on. Each of these /24 subnets would have its own network address, broadcast address, and range of usable IPs.
Example Scenario:
Let's say you have an IP address 172.16.10.50 and a CIDR prefix length of /20.
- IP Address: 172.16.10.50
- CIDR Prefix: /20
- Subnet Mask: 255.255.240.0 (because the first 20 bits are 1s)
- Network Address: 172.16.0.0 (The first 20 bits of 172.16.10.50 are 10101100.00010000.0000, and the remaining 12 host bits are set to 0)
- Broadcast Address: 172.16.15.255 (The first 20 bits are 10101100.00010000.0000, and the remaining 12 host bits are set to 1)
- First Usable IP: 172.16.0.1
- Last Usable IP: 172.16.15.254
- Total Hosts: 2^(32-20) = 2^12 = 4096
- Usable Hosts: 4096 – 2 = 4094
This calculator helps you quickly determine these critical network parameters, making network planning and troubleshooting much easier.