IP Subnet Address Calculator
:root {
–primary-blue: #004a99;
–success-green: #28a745;
–light-background: #f8f9fa;
–white: #ffffff;
–dark-text: #343a40;
–border-color: #dee2e6;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: var(–light-background);
color: var(–dark-text);
line-height: 1.6;
margin: 0;
padding: 20px;
}
.calculator-container {
max-width: 800px;
margin: 30px auto;
background-color: var(–white);
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
padding: 30px;
border: 1px solid var(–border-color);
}
h1, h2 {
color: var(–primary-blue);
text-align: center;
margin-bottom: 20px;
}
.input-section, .output-section {
margin-bottom: 30px;
padding: 20px;
border: 1px solid var(–border-color);
border-radius: 5px;
background-color: var(–white);
}
.input-group {
margin-bottom: 15px;
display: flex;
flex-wrap: wrap;
align-items: center;
}
.input-group label {
flex: 1 1 150px; /* Grow, Shrink, Basis */
margin-right: 15px;
font-weight: 500;
color: var(–primary-blue);
}
.input-group input[type="text"],
.input-group select {
flex: 2 1 200px; /* Grow, Shrink, Basis */
padding: 10px 12px;
border: 1px solid var(–border-color);
border-radius: 4px;
box-sizing: border-box;
font-size: 1rem;
}
.input-group input[type="text"]:focus,
.input-group select:focus {
outline: none;
border-color: var(–primary-blue);
box-shadow: 0 0 0 2px rgba(0, 74, 153, 0.2);
}
button {
display: block;
width: 100%;
padding: 12px 20px;
background-color: var(–primary-blue);
color: var(–white);
border: none;
border-radius: 4px;
font-size: 1.1rem;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s ease;
margin-top: 10px;
}
button:hover {
background-color: #003366;
}
#result {
background-color: var(–success-green);
color: var(–white);
padding: 25px;
border-radius: 5px;
text-align: center;
font-size: 1.4rem;
font-weight: bold;
box-shadow: 0 2px 8px rgba(40, 167, 69, 0.3);
}
#result p {
margin: 5px 0;
}
.article-section {
margin-top: 40px;
background-color: var(–white);
padding: 30px;
border-radius: 8px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
border: 1px solid var(–border-color);
}
.article-section h2 {
text-align: left;
margin-bottom: 15px;
}
.article-section p, .article-section ul, .article-section li, .article-section strong {
color: var(–dark-text);
}
.article-section ul {
padding-left: 20px;
}
.article-section li {
margin-bottom: 10px;
}
.article-section code {
background-color: var(–light-background);
padding: 2px 5px;
border-radius: 3px;
font-family: 'Courier New', Courier, monospace;
}
/* Responsive adjustments */
@media (max-width: 600px) {
.input-group {
flex-direction: column;
align-items: stretch;
}
.input-group label {
margin-bottom: 8px;
margin-right: 0;
text-align: center;
}
.input-group input[type="text"],
.input-group select {
width: 100%;
}
.calculator-container {
padding: 20px;
}
h1 {
font-size: 1.8rem;
}
#result {
font-size: 1.2rem;
}
}
IP Subnet Address Calculator
Results
Enter an IP address and CIDR mask to see subnet details.
Understanding IP Subnetting
IP subnetting is a fundamental networking technique used to divide a large IP network into smaller, more manageable sub-networks, called subnets. This process is crucial for efficient network management, improving security, and optimizing network performance. Each subnet is a logical subdivision of an IP address space.
Why Subnet?
- Network Organization: Divides a large network into smaller, logical segments (e.g., by department, floor, or function).
- Security: Allows for the implementation of security policies between subnets, controlling traffic flow.
- Performance: Reduces broadcast traffic within a network. Broadcasts are contained within their subnet, leading to less congestion.
- IP Address Conservation: Allows for more efficient allocation of IP addresses, especially in the days of IPv4 scarcity.
The Math Behind Subnetting
Subnetting involves borrowing bits from the host portion of an IP address to create subnet bits. This is typically done by using a subnet mask.
An IPv4 address is a 32-bit number, typically represented in dotted-decimal notation (e.g., 192.168.1.0). It consists of two parts: the Network ID and the Host ID. A subnet mask is also a 32-bit number that helps distinguish between the network portion and the host portion.
The CIDR (Classless Inter-Domain Routing) notation is a more modern and flexible way to represent subnet masks. It uses a slash followed by the number of network bits (e.g., /24). A /24 mask means the first 24 bits are part of the network ID, and the remaining 8 bits are for host IDs.
Example Calculation Breakdown (for 192.168.1.0 /24):
- IP Address: 192.168.1.0
- CIDR Mask: /24
- Subnet Mask (Dotted Decimal): 255.255.255.0 (This is the binary representation of 24 consecutive '1' bits followed by 8 '0' bits)
- Network Address: This is the first address in the subnet. It's calculated by performing a bitwise AND operation between the IP address and the subnet mask. For
192.168.1.0 /24, the network address is 192.168.1.0.
- Broadcast Address: This is the last address in the subnet, used to send data to all hosts within that subnet. It's calculated by taking the bitwise NOT of the subnet mask and ORing it with the network address. For
192.168.1.0 /24, the broadcast address is 192.168.1.255.
- Usable Host Addresses: The range of IP addresses available for devices on the network. It's from the Network Address + 1 up to the Broadcast Address – 1.
- Total Addresses: Calculated as 2(32 – CIDR value). For
/24, it's 2(32-24) = 28 = 256 addresses.
- Usable Hosts: Total Addresses – 2 (for network and broadcast addresses). For
/24, it's 256 – 2 = 254 usable hosts.
This calculator simplifies these calculations, providing a quick way to determine the network address, broadcast address, and the range of usable IP addresses for any given network segment.
function calculateSubnet() {
var ipAddress = document.getElementById("ipAddress").value;
var cidrMaskStr = document.getElementById("cidrMask").value.replace('/', "); // Remove potential slash
var resultDiv = document.getElementById("result");
resultDiv.innerHTML = 'Calculating…';
// Basic IP Address Validation (IPv4)
var ipParts = ipAddress.split('.');
if (ipParts.length !== 4) {
resultDiv.innerHTML = 'Error: Invalid IP Address format.';
return;
}
for (var i = 0; i < ipParts.length; i++) {
var part = parseInt(ipParts[i], 10);
if (isNaN(part) || part 255) {
resultDiv.innerHTML = 'Error: Invalid IP Address octet (must be 0-255).';
return;
}
}
// CIDR Mask Validation
var cidrMask = parseInt(cidrMaskStr, 10);
if (isNaN(cidrMask) || cidrMask 32) {
resultDiv.innerHTML = 'Error: Invalid CIDR mask (must be 0-32).';
return;
}
// — Core Subnetting Logic —
// Convert IP to binary string for easier manipulation
var ipBinary = ipParts.map(function(part) {
return parseInt(part, 10).toString(2).padStart(8, '0');
}).join(");
// Create subnet mask binary string
var subnetMaskBinary = '1'.repeat(cidrMask) + '0'.repeat(32 – cidrMask);
// Calculate Network Address Binary
var networkAddressBinary = ";
for (var i = 0; i < 32; i++) {
networkAddressBinary += (parseInt(ipBinary[i], 2) & parseInt(subnetMaskBinary[i], 2)).toString();
}
// Calculate Broadcast Address Binary
var broadcastAddressBinary = '';
for (var i = 0; i < 32; i++) {
broadcastAddressBinary += (parseInt(ipBinary[i], 2) | parseInt('1'.repeat(cidrMask) + '0'.repeat(32 – cidrMask), 2)).toString();
}
// Convert binary results back to dotted-decimal
function binaryToDottedDecimal(binaryString) {
var octets = [];
for (var i = 0; i < 32; i += 8) {
octets.push(parseInt(binaryString.substring(i, i + 8), 2));
}
return octets.join('.');
}
var networkAddress = binaryToDottedDecimal(networkAddressBinary);
var broadcastAddress = binaryToDottedDecimal(broadcastAddressBinary);
// Calculate total and usable hosts
var totalAddresses = Math.pow(2, (32 – cidrMask));
var usableHosts = totalAddresses – 2;
// Determine host address range
var startHostBinary = networkAddressBinary.substring(0, 31) + '1';
var endHostBinary = broadcastAddressBinary.substring(0, 31) + '0';
var firstUsableHost = binaryToDottedDecimal(startHostBinary);
var lastUsableHost = binaryToDottedDecimal(endHostBinary);
// Special cases for /31 and /32
if (cidrMask === 32) {
firstUsableHost = networkAddress;
lastUsableHost = networkAddress;
usableHosts = 1; // A /32 technically has 1 usable host, itself
} else if (cidrMask === 31) {
firstUsableHost = networkAddress;
lastUsableHost = broadcastAddress;
usableHosts = 2; // A /31 has 2 usable hosts, typically for point-to-point links
}
// Display results
resultDiv.innerHTML =
'Network Address: ' + networkAddress + '' +
'Broadcast Address: ' + broadcastAddress + '' +
'Subnet Mask: ' + binaryToDottedDecimal('1'.repeat(cidrMask) + '0'.repeat(32 – cidrMask)) + ' (/' + cidrMask + ')' +
'Total Addresses: ' + totalAddresses + '' +
'Usable Host Range: ' + firstUsableHost + ' – ' + lastUsableHost + '' +
'Number of Usable Hosts: ' + usableHosts + '';
}