Enter network address and subnet mask to see results.
Understanding Subnetting and the Submask Calculator
Subnetting is a fundamental networking technique that allows administrators to divide a large IP network into smaller, more manageable sub-networks (subnets). This process offers several benefits, including improved network performance, enhanced security, and more efficient use of IP addresses. The Submask Calculator helps determine the network, broadcast, and usable host addresses for a given network and subnet mask.
How it Works: The Math Behind Subnetting
IP addresses and subnet masks are represented in dotted-decimal notation (e.g., 192.168.1.0 and 255.255.255.0). To perform subnetting calculations, it's often easier to convert these into their binary equivalents.
A subnet mask is used to divide an IP address into two parts: the network portion and the host portion. The bits that are set to '1' in the subnet mask correspond to the network portion, and the bits set to '0' correspond to the host portion.
Network Address: This is the first IP address in a subnet. It's calculated by performing a bitwise AND operation between the IP address and the subnet mask. All host bits in the resulting IP address are set to 0.
Broadcast Address: This is the last IP address in a subnet. It's calculated by taking the network address and setting all the host bits (the bits corresponding to the '0's in the subnet mask) to '1'.
Usable Host Addresses: These are the IP addresses between the network address and the broadcast address that can be assigned to devices. The number of usable hosts is 2(number of host bits) – 2. The "-2" accounts for the network address itself and the broadcast address, which cannot be assigned to individual hosts.
Example Calculation:
Let's take the example inputs:
Network Address: 192.168.1.50
Subnet Mask: 255.255.255.192
1. Convert to Binary:
IP Address (192.168.1.50): 11000000.10101000.00000001.00110010
Subnet Mask (255.255.255.192): 11111111.11111111.11111111.11000000
2. Identify Network and Host Bits:
The subnet mask has 26 bits set to '1' (network bits) and 6 bits set to '0' (host bits).
3. Calculate Network Address:
Perform a bitwise AND between the IP address and the subnet mask.
IP: 11000000.10101000.00000001.00110010
Mask: 11111111.11111111.11111111.11000000
AND: 11000000.10101000.00000001.00000000
Binary 11000000.10101000.00000001.00000000 converts to 192.168.1.0.
So, the Network Address is 192.168.1.0.
4. Calculate Broadcast Address:
Take the network address and set all host bits to '1'.
Network:11000000.10101000.00000001.00000000
Host Bits to Flip: 00000000.00000000.00000000.00111111
Result: 11000000.10101000.00000001.00111111
Binary 11000000.10101000.00000001.00111111 converts to 192.168.1.63.
So, the Broadcast Address is 192.168.1.63.
5. Calculate Usable Host Addresses:
Number of host bits = 6.
Total hosts = 26 = 64.
Usable hosts = 64 – 2 = 62.
The usable IP range is from 192.168.1.1 to 192.168.1.62.
function ipToBinary(ipAddress) {
var parts = ipAddress.split('.');
var binaryParts = [];
for (var i = 0; i < parts.length; i++) {
var partInt = parseInt(parts[i]);
if (isNaN(partInt) || partInt 255) {
return null; // Invalid IP octet
}
var binary = partInt.toString(2);
binary = '00000000'.slice(binary.length) + binary;
binaryParts.push(binary);
}
return binaryParts.join('.');
}
function binaryToIp(binaryString) {
var binaryParts = binaryString.split('.');
var ipParts = [];
for (var i = 0; i < binaryParts.length; i++) {
if (binaryParts[i].length !== 8) return null; // Invalid binary segment length
var octet = parseInt(binaryParts[i], 2);
if (isNaN(octet)) return null;
ipParts.push(octet);
}
return ipParts.join('.');
}
function calculateSubmask() {
var ipAddress = document.getElementById("networkAddress").value;
var subnetMask = document.getElementById("subnetMask").value;
var resultDiv = document.getElementById("result-value");
var ipBinary = ipToBinary(ipAddress);
var maskBinary = ipToBinary(subnetMask);
if (!ipBinary) {
resultDiv.innerHTML = "Invalid Network Address format.";
return;
}
if (!maskBinary) {
resultDiv.innerHTML = "Invalid Subnet Mask format.";
return;
}
var ipBinaryParts = ipBinary.split('.');
var maskBinaryParts = maskBinary.split('.');
var networkBinaryParts = [];
var hostBitsCount = 0;
for (var i = 0; i < 4; i++) {
var currentMaskPart = maskBinaryParts[i];
var currentIpPart = ipBinaryParts[i];
var networkPart = "";
for (var j = 0; j < 8; j++) {
if (currentMaskPart[j] === '1') {
networkPart += currentIpPart[j];
} else {
networkPart += '0';
hostBitsCount++;
}
}
networkBinaryParts.push(networkPart);
}
var networkAddressBinary = networkBinaryParts.join('.');
var networkAddress = binaryToIp(networkAddressBinary);
if (!networkAddress) {
resultDiv.innerHTML = "Calculation Error: Could not determine Network Address.";
return;
}
var broadcastBinaryParts = [];
var networkBinaryString = networkAddressBinary.replace(/\./g, ''); // Remove dots for easier manipulation
// Create broadcast address binary by setting host bits to 1
var broadcastBinaryString = networkBinaryString.substring(0, 32 – hostBitsCount) + '1'.repeat(hostBitsCount);
// Re-add dots for binaryToIp function
var broadcastBinaryWithDots = "";
for(var k = 0; k < broadcastBinaryString.length; k += 8) {
broadcastBinaryWithDots += broadcastBinaryString.substring(k, k + 8) + ".";
}
broadcastBinaryWithDots = broadcastBinaryWithDots.slice(0, -1); // Remove trailing dot
var broadcastAddress = binaryToIp(broadcastBinaryWithDots);
if (!broadcastAddress) {
resultDiv.innerHTML = "Calculation Error: Could not determine Broadcast Address.";
return;
}
var totalHosts = Math.pow(2, hostBitsCount);
var usableHosts = totalHosts – 2;
var resultHtml = "Network Address: " + networkAddress + "";
resultHtml += "Broadcast Address: " + broadcastAddress + "";
resultHtml += "Total Hosts in Subnet: " + totalHosts + "";
resultHtml += "Usable Host IPs: " + (usableHosts < 0 ? 0 : usableHosts);
resultDiv.innerHTML = resultHtml;
}