Subnet Mask Calculator
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f8f9fa;
color: #333;
line-height: 1.6;
margin: 0;
padding: 0;
}
.loan-calc-container {
max-width: 800px;
margin: 40px auto;
padding: 30px;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}
h1, h2 {
color: #004a99;
text-align: center;
margin-bottom: 20px;
}
.input-group {
margin-bottom: 20px;
display: flex;
flex-direction: column;
}
.input-group label {
margin-bottom: 8px;
font-weight: bold;
color: #555;
}
.input-group input[type="text"],
.input-group select {
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 1rem;
transition: border-color 0.3s ease;
}
.input-group input[type="text"]:focus,
.input-group select:focus {
border-color: #004a99;
outline: none;
}
button {
display: block;
width: 100%;
padding: 15px;
background-color: #004a99;
color: white;
border: none;
border-radius: 5px;
font-size: 1.1rem;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 20px;
}
button:hover {
background-color: #003366;
}
#result {
margin-top: 30px;
padding: 25px;
background-color: #e7f3ff; /* Light blue background for results */
border: 1px solid #004a99;
border-radius: 5px;
text-align: center;
font-size: 1.2rem;
font-weight: bold;
color: #003366;
}
#result p {
margin: 5px 0;
}
.article-section {
margin-top: 40px;
padding: 25px;
background-color: #ffffff;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}
.article-section h2 {
text-align: left;
margin-bottom: 15px;
}
.article-section p, .article-section ul, .article-section li {
margin-bottom: 15px;
}
.article-section code {
background-color: #e7f0fa;
padding: 3px 6px;
border-radius: 3px;
font-family: Consolas, Monaco, 'Andale Mono', 'Ubuntu Mono', monospace;
}
@media (max-width: 600px) {
.loan-calc-container {
margin: 20px;
padding: 20px;
}
button {
font-size: 1rem;
padding: 12px;
}
#result {
font-size: 1.1rem;
}
}
Subnet Mask Calculator
Enter network address and CIDR notation to see subnet details.
Understanding Subnet Masks and CIDR Notation
Subnetting is a fundamental networking technique that divides a larger network into smaller, more manageable subnetworks. This is crucial for improving network performance, security, and organization. A subnet mask is a 32-bit number that divides an IP address into two parts: the network portion and the host portion. The CIDR (Classless Inter-Domain Routing) notation provides a shorthand way to represent the subnet mask, indicating how many bits are used for the network portion.
An IPv4 address is typically represented in the dot-decimal format (e.g., 192.168.1.100). A subnet mask, when applied, determines which part of the IP address identifies the network and which part identifies a specific device (host) within that network. For example, a Class C network often uses a subnet mask of 255.255.255.0.
How CIDR Notation Works
CIDR notation simplifies the representation of subnet masks. Instead of writing out the full 32-bit mask (like 255.255.255.0), we use a slash followed by the number of bits that are set to '1' in the subnet mask.
- A CIDR of
/24 means the first 24 bits are for the network portion, and the remaining 8 bits are for the host portion. This corresponds to the subnet mask 255.255.255.0.
- A CIDR of
/26 means the first 26 bits are for the network, and the remaining 6 bits are for the host. This mask is 255.255.255.192.
- A CIDR of
/16 means the first 16 bits are for the network, and the remaining 16 bits are for the host. This corresponds to the subnet mask 255.255.0.0.
Calculating Subnet Details
To calculate subnet details, we need the IP address (specifically the network address of the subnet) and its CIDR notation. The calculator uses this information to determine:
- Subnet Mask: The 32-bit number that defines network and host portions.
- Network Address: The first address in the subnet, used to identify the network itself.
- Broadcast Address: The last address in the subnet, used to send data to all hosts within that subnet.
- Usable IP Range: The range of IP addresses that can be assigned to devices (hosts) within the subnet.
- Total Hosts: The total number of possible IP addresses in the subnet (including network and broadcast).
- Usable Hosts: The number of IP addresses available for actual devices (Total Hosts – 2).
Why Subnetting is Important
Subnetting is vital for:
- Performance Improvement: Reduces broadcast traffic by segmenting the network.
- Security Enhancement: Allows for the implementation of access control lists (ACLs) and firewall rules between subnets.
- Network Organization: Makes it easier to manage and troubleshoot network devices based on their location or function.
- IP Address Conservation: While not its primary goal, efficient subnetting can help utilize available IP address space more effectively.
function calculateSubnet() {
var networkAddress = document.getElementById("networkAddress").value;
var cidrNotationStr = document.getElementById("cidrNotation").value;
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = ""; // Clear previous results
// Validate CIDR input
var cidrNotation = parseInt(cidrNotationStr, 10);
if (isNaN(cidrNotation) || cidrNotation 32) {
resultDiv.innerHTML = "Please enter a valid CIDR notation between 0 and 32.";
return;
}
// Validate Network Address format
var ipParts = networkAddress.split('.');
if (ipParts.length !== 4) {
resultDiv.innerHTML = "Invalid Network Address format. Please use dot-decimal notation (e.g., 192.168.1.0).";
return;
}
for (var i = 0; i < ipParts.length; i++) {
var part = parseInt(ipParts[i], 10);
if (isNaN(part) || part 255) {
resultDiv.innerHTML = "Invalid IP address part. Each part must be between 0 and 255.";
return;
}
}
// — Calculations —
// 1. Calculate Subnet Mask
var subnetMaskParts = [0, 0, 0, 0];
var bitsRemaining = cidrNotation;
for (var i = 0; i = 8) {
subnetMaskParts[i] = 255;
bitsRemaining -= 8;
} else {
subnetMaskParts[i] = (256 – Math.pow(2, 8 – bitsRemaining));
bitsRemaining = 0;
}
}
var subnetMask = subnetMaskParts.join('.');
// 2. Calculate Network Address (using bitwise AND, but for simplicity, we assume the input *is* the network address and will derive host bits from it)
// In a more complex calculator, we'd parse the IP and mask to find the *true* network address.
// For this calculator, we'll use the provided networkAddress and just ensure it's valid for the CIDR.
// A more robust check would involve converting IP to binary and ANDing with mask.
// For this scope, we'll present the input as the Network Address if valid.
// 3. Calculate Broadcast Address
var networkParts = networkAddress.split('.').map(Number);
var broadcastParts = [0,0,0,0];
var subnetMaskPartsNum = subnetMask.split('.').map(Number);
var hostBits = 32 – cidrNotation;
// Set network bits
for(var i = 0; i 0) {
var lastOctetMask = 255 – subnetMaskPartsNum[3];
var lastOctetHostBits = Math.min(hostBits, 8);
var lastOctetHostMask = Math.pow(2, lastOctetHostBits) – 1;
if (hostBits <= 8) {
broadcastParts[3] = networkParts[3] | lastOctetHostMask;
} else if (hostBits <= 16) { // applies to 3rd octet
var thirdOctetHostBits = hostBits – 8;
var thirdOctetHostMask = Math.pow(2, thirdOctetHostBits) – 1;
broadcastParts[3] = 255; // All host bits in last octet are 1
broadcastParts[2] = (networkParts[2] | thirdOctetHostMask) & 255;
} else if (hostBits <= 24) { // applies to 2nd octet
var secondOctetHostBits = hostBits – 16;
var secondOctetHostMask = Math.pow(2, secondOctetHostBits) – 1;
broadcastParts[3] = 255;
broadcastParts[2] = 255;
broadcastParts[1] = (networkParts[1] | secondOctetHostMask) & 255;
} else { // applies to 1st octet
var firstOctetHostBits = hostBits – 24;
var firstOctetHostMask = Math.pow(2, firstOctetHostBits) – 1;
broadcastParts[3] = 255;
broadcastParts[2] = 255;
broadcastParts[1] = 255;
broadcastParts[0] = (networkParts[0] | firstOctetHostMask) & 255;
}
}
var broadcastAddress = broadcastParts.join('.');
// 4. Calculate Total Hosts and Usable Hosts
var totalHosts = Math.pow(2, 32 – cidrNotation);
var usableHosts = totalHosts – 2;
if (usableHosts < 0) usableHosts = 0; // Handle /31 and /32 cases
// 5. Calculate Usable IP Range
var usableStartParts = […networkParts];
var usableEndParts = […broadcastParts];
// For the start, increment the last octet of the network address
if (cidrNotation < 32) { // If not a /32 network
usableStartParts[3]++;
if (usableStartParts[3] === 256) { // Carry over
usableStartParts[3] = 0;
usableStartParts[2]++;
if (usableStartParts[2] === 256) {
usableStartParts[2] = 0;
usableStartParts[1]++;
if (usableStartParts[1] === 256) {
usableStartParts[1] = 0;
usableStartParts[0]++;
}
}
}
} else { // For /32, there are no usable hosts
usableStartParts = ['N/A', 'N/A', 'N/A', 'N/A'];
}
var usableStartAddress = usableStartParts.join('.');
// For the end, decrement the last octet of the broadcast address
if (cidrNotation < 32) { // If not a /32 network
usableEndParts[3]–;
if (usableEndParts[3] < 0) { // Carry over from borrowing
usableEndParts[3] = 255;
usableEndParts[2]–;
if (usableEndParts[2] < 0) {
usableEndParts[2] = 255;
usableEndParts[1]–;
if (usableEndParts[1] < 0) {
usableEndParts[1] = 255;
usableEndParts[0]–;
}
}
}
} else { // For /32, there are no usable hosts
usableEndParts = ['N/A', 'N/A', 'N/A', 'N/A'];
}
var usableEndAddress = usableEndParts.join('.');
// Ensure network and broadcast addresses are correctly derived if input IP was not strictly the network address
// This part is simplified: we are treating the input `networkAddress` as the network address.
// A truly robust calculator would parse the input IP and apply the mask.
// For this exercise, we trust the user inputs a valid network address for the given CIDR or we calculate based on it.
// The calculated broadcast needs careful handling when the input IP isn't precisely aligned with the mask bits.
// Re-calculating broadcast based on the *derived* network address from input IP is more robust.
// However, given the input is `networkAddress`, we'll proceed assuming it's correct and adjust broadcast.
// Correct network address and broadcast address derivation if the input was just an IP and not the network address
var ipBinary = ipParts.map(function(part) { return parseInt(part, 10).toString(2).padStart(8, '0'); }).join('');
var maskBinary = subnetMaskParts.map(function(part) { return part.toString(2).padStart(8, '0'); }).join('');
var networkBinary = "";
var broadcastBinary = "";
for (var i = 0; i < 32; i++) {
if (maskBinary[i] === '1') {
networkBinary += ipBinary[i];
broadcastBinary += ipBinary[i];
} else {
networkBinary += '0';
broadcastBinary += '1';
}
}
var finalNetworkParts = [];
var finalBroadcastParts = [];
for (var i = 0; i < 4; i++) {
finalNetworkParts.push(parseInt(networkBinary.substring(i * 8, (i + 1) * 8), 2));
finalBroadcastParts.push(parseInt(broadcastBinary.substring(i * 8, (i + 1) * 8), 2));
}
var correctNetworkAddress = finalNetworkParts.join('.');
var correctBroadcastAddress = finalBroadcastParts.join('.');
// Recalculate usable start/end based on correct network/broadcast
var correctUsableStartParts = […finalNetworkParts];
var correctUsableEndParts = […finalBroadcastParts];
if (cidrNotation < 32) {
correctUsableStartParts[3]++;
if (correctUsableStartParts[3] === 256) {
correctUsableStartParts[3] = 0;
correctUsableStartParts[2]++;
if (correctUsableStartParts[2] === 256) {
correctUsableStartParts[2] = 0;
correctUsableStartParts[1]++;
if (correctUsableStartParts[1] === 256) {
correctUsableStartParts[1] = 0;
correctUsableStartParts[0]++;
}
}
}
correctUsableEndParts[3]–;
if (correctUsableEndParts[3] < 0) {
correctUsableEndParts[3] = 255;
correctUsableEndParts[2]–;
if (correctUsableEndParts[2] < 0) {
correctUsableEndParts[2] = 255;
correctUsableEndParts[1]–;
if (correctUsableEndParts[1] < 0) {
correctUsableEndParts[1] = 255;
correctUsableEndParts[0]–;
}
}
}
} else { // For /32
correctUsableStartParts = ['N/A', 'N/A', 'N/A', 'N/A'];
correctUsableEndParts = ['N/A', 'N/A', 'N/A', 'N/A'];
}
var correctUsableStartAddress = correctUsableStartParts.join('.');
var correctUsableEndAddress = correctUsableEndParts.join('.');
resultDiv.innerHTML = `
Subnet Mask: ${subnetMask}
Network Address: ${correctNetworkAddress}
Broadcast Address: ${correctBroadcastAddress}
Usable IP Range: ${correctUsableStartAddress} – ${correctUsableEndAddress}
Total Addresses in Subnet: ${totalHosts}
Usable Host Addresses: ${usableHosts}
`;
}