Use this tool to quickly determine the network address, broadcast address, subnet mask, and usable host range for any given IP address and CIDR prefix.
.netmask-calculator-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f9f9f9;
border: 1px solid #ddd;
border-radius: 8px;
padding: 25px;
max-width: 600px;
margin: 30px auto;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
color: #333;
}
.netmask-calculator-container h2 {
text-align: center;
color: #0056b3;
margin-bottom: 20px;
font-size: 1.8em;
}
.netmask-calculator-container p {
text-align: center;
margin-bottom: 25px;
line-height: 1.6;
color: #555;
}
.calculator-inputs label {
display: block;
margin-bottom: 8px;
font-weight: bold;
color: #444;
}
.calculator-inputs input[type="text"],
.calculator-inputs input[type="number"] {
width: calc(100% – 22px);
padding: 12px;
margin-bottom: 20px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 1em;
box-sizing: border-box;
}
.calculator-inputs input[type="text"]:focus,
.calculator-inputs input[type="number"]:focus {
border-color: #007bff;
outline: none;
box-shadow: 0 0 5px rgba(0, 123, 255, 0.3);
}
.calculator-inputs button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: #007bff;
color: white;
border: none;
border-radius: 5px;
font-size: 1.1em;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.2s ease;
margin-top: 10px;
}
.calculator-inputs button:hover {
background-color: #0056b3;
transform: translateY(-2px);
}
.calculator-inputs button:active {
transform: translateY(0);
}
.calculator-results {
margin-top: 30px;
padding: 20px;
background-color: #e9f7ff;
border: 1px solid #b3e0ff;
border-radius: 8px;
display: none; /* Hidden by default, shown after calculation */
}
.calculator-results h3 {
color: #0056b3;
margin-top: 0;
margin-bottom: 15px;
font-size: 1.5em;
text-align: center;
}
.calculator-results p {
margin-bottom: 10px;
font-size: 1.05em;
color: #333;
text-align: left;
}
.calculator-results p strong {
color: #0056b3;
min-width: 150px;
display: inline-block;
}
.calculator-results .error {
color: #dc3545;
font-weight: bold;
text-align: center;
}
function calculateNetmask() {
var ipAddressInput = document.getElementById("ipAddress").value;
var cidrPrefixInput = document.getElementById("cidrPrefix").value;
var resultDiv = document.getElementById("netmaskResult");
resultDiv.style.display = "block"; // Show the result div
// Helper function to convert a 32-bit integer to dotted decimal IP
function intToDottedDecimal(ipInt) {
var octet4 = ipInt & 255;
var octet3 = (ipInt >> 8) & 255;
var octet2 = (ipInt >> 16) & 255;
var octet1 = (ipInt >> 24) & 255;
return octet1 + "." + octet2 + "." + octet3 + "." + octet4;
}
// Validate IP Address
var ipParts = ipAddressInput.split('.');
if (ipParts.length !== 4) {
resultDiv.innerHTML = "Invalid IP Address format. Please use X.X.X.X";
return;
}
var ipInt = 0;
for (var i = 0; i < 4; i++) {
var octet = parseInt(ipParts[i], 10);
if (isNaN(octet) || octet 255) {
resultDiv.innerHTML = "Invalid IP Address octet: " + ipParts[i] + ". Must be between 0 and 255.";
return;
}
ipInt = (ipInt << 8) | octet;
}
// Validate CIDR Prefix
var cidrPrefix = parseInt(cidrPrefixInput, 10);
if (isNaN(cidrPrefix) || cidrPrefix 32) {
resultDiv.innerHTML = "Invalid CIDR Prefix. Must be between 0 and 32.";
return;
}
// Calculate Subnet Mask (32-bit integer)
var subnetMaskInt = -1 << (32 – cidrPrefix);
// Calculate Network Address (32-bit integer)
var networkAddressInt = ipInt & subnetMaskInt;
// Calculate Broadcast Address (32-bit integer)
var broadcastAddressInt = networkAddressInt | (~subnetMaskInt);
// Calculate Number of Hosts
var numHosts = Math.pow(2, (32 – cidrPrefix));
var usableHosts = 0;
if (cidrPrefix 0) {
firstUsableHostInt = networkAddressInt + 1;
} else if (cidrPrefix === 31) { // For /31, the two addresses are usable as point-to-point
firstUsableHostInt = networkAddressInt;
} else if (cidrPrefix === 32) { // For /32, the single address is usable
firstUsableHostInt = networkAddressInt;
}
// Calculate Last Usable Host
var lastUsableHostInt = 0;
if (usableHosts > 0) {
lastUsableHostInt = broadcastAddressInt – 1;
} else if (cidrPrefix === 31) { // For /31, the two addresses are usable as point-to-point
lastUsableHostInt = broadcastAddressInt;
} else if (cidrPrefix === 32) { // For /32, the single address is usable
lastUsableHostInt = broadcastAddressInt;
}
// Convert all to dotted decimal
var subnetMaskDotted = intToDottedDecimal(subnetMaskInt);
var networkAddressDotted = intToDottedDecimal(networkAddressInt);
var broadcastAddressDotted = intToDottedDecimal(broadcastAddressInt);
var firstUsableHostDotted = (usableHosts > 0 || cidrPrefix >= 31) ? intToDottedDecimal(firstUsableHostInt) : "N/A";
var lastUsableHostDotted = (usableHosts > 0 || cidrPrefix >= 31) ? intToDottedDecimal(lastUsableHostInt) : "N/A";
// Display results
var resultsHtml = "
A Netmask Calculator is an essential tool for network administrators, IT professionals, and anyone involved in network planning and configuration. It helps in understanding how IP addresses are divided into network and host portions, and how subnetting works.
What is an IP Address?
An Internet Protocol (IP) address is a numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. The most common version, IPv4, uses 32-bit addresses, typically represented in dotted-decimal notation (e.g., 192.168.1.1).
What is a Subnet Mask?
A subnet mask is a 32-bit number that separates the IP address into the network address and the host address. It works by performing a bitwise AND operation with the IP address. The network portion of the IP address identifies the specific network a device belongs to, while the host portion identifies the specific device within that network.
For example, a subnet mask of 255.255.255.0 (or /24 in CIDR notation) means the first 24 bits of the IP address represent the network, and the remaining 8 bits represent the host.
What is CIDR?
CIDR stands for Classless Inter-Domain Routing. It's a method for allocating IP addresses and routing IP packets. Instead of using traditional IP classes (A, B, C), CIDR uses a variable-length subnet mask. The CIDR prefix (e.g., /24) indicates the number of bits in the IP address that represent the network portion. This allows for more efficient use of IP address space and more flexible subnetting.
Key Terms Explained by the Calculator:
IP Address: The unique identifier for a device on a network.
CIDR Prefix: The number of bits used for the network portion of the IP address.
Subnet Mask: The 32-bit mask that defines the network and host portions. It's derived directly from the CIDR prefix.
Network Address: The first address in a subnet. It identifies the network itself and is typically not assigned to a host. All host bits are zero.
Broadcast Address: The last address in a subnet. It's used to send data to all devices within that specific network. All host bits are one.
Total Hosts: The total number of IP addresses available within the subnet, including the network and broadcast addresses. Calculated as 2^(32 – CIDR prefix).
Usable Hosts: The number of IP addresses that can be assigned to devices within the subnet. For most subnets, this is Total Hosts – 2 (excluding the network and broadcast addresses). Special cases like /31 and /32 have 0 usable hosts in this traditional sense, though /31 can be used for point-to-point links.
First Usable Host: The first IP address in the subnet that can be assigned to a device (Network Address + 1).
Last Usable Host: The last IP address in the subnet that can be assigned to a device (Broadcast Address – 1).
How to Use the Calculator:
Enter IP Address: Input the IP address you want to analyze (e.g., 192.168.1.10).
Enter CIDR Prefix: Input the CIDR prefix (e.g., 24 for a /24 network).
Click "Calculate Netmask": The calculator will instantly display the network address, broadcast address, subnet mask, and the range of usable IP addresses for hosts within that subnet.
Example Scenario:
Imagine you have an IP address 172.16.10.50 and a CIDR prefix of /20. Using the calculator, you would find:
IP Address: 172.16.10.50/20
Subnet Mask: 255.255.240.0
Network Address: 172.16.0.0
Broadcast Address: 172.16.15.255
Total Hosts: 4096
Usable Hosts: 4094
First Usable Host: 172.16.0.1
Last Usable Host: 172.16.15.254
This information tells you that your device 172.16.10.50 is part of the 172.16.0.0 network, and any device with an IP between 172.16.0.1 and 172.16.15.254 can communicate directly within this subnet.