Subnet Mask / CIDR Calculator

ANALife Services AuthorityNational Calculator Authority›Subnet Mask / CIDR Calculator

.calc-container { max-width: 640px; margin: 2rem 0; padding: 1.5rem; background: #fff; border: 1px solid #ddd; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.06); font-family: system-ui, -apple-system, sans-serif; } .calc-container h3 { font-family: Georgia, serif; font-size: 1.15rem; color: #1a1a1a; margin-bottom: 1rem; padding-bottom: 0.5rem; border-bottom: 2px solid var(--ac, #3d5a80); } .calc-row { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.75rem; flex-wrap: wrap; } .calc-row label { min-width: 160px; font-size: 0.9rem; color: #333; font-weight: 500; } .calc-row input[type="number"], .calc-row select { flex: 1; min-width: 120px; max-width: 200px; padding: 0.5rem 0.6rem; border: 1px solid #ccc; border-radius: 4px; font-size: 0.9rem; font-family: system-ui, sans-serif; color: #1a1a1a; background: #fafaf8; } .calc-row input:focus, .calc-row select:focus { outline: none; border-color: var(--ac, #3d5a80); box-shadow: 0 0 0 2px rgba(26,74,138,0.12); } .calc-row .unit { font-size: 0.82rem; color: #888; min-width: 30px; } .calc-btn { display: inline-block; margin-top: 0.5rem; padding: 0.55rem 1.5rem; background: var(--ac, #3d5a80); color: #fff; border: none; border-radius: 4px; font-size: 0.9rem; font-weight: 600; cursor: pointer; font-family: system-ui, sans-serif; } .calc-btn:hover { opacity: 0.9; } .calc-result { margin-top: 1.25rem; padding: 1rem 1.25rem; background: #f0f6fc; border-left: 3px solid var(--ac, #3d5a80); border-radius: 0 6px 6px 0; display: none; } .calc-result.visible { display: block; } .calc-result-label { font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.06em; color: #666; margin-bottom: 0.25rem; } .calc-result-value { font-size: 1.6rem; font-weight: 700; color: var(--ac, #3d5a80); } .calc-result-detail { font-size: 0.85rem; color: #555; margin-top: 0.5rem; line-height: 1.5; } .calc-note { margin-top: 1rem; font-size: 0.8rem; color: #888; font-style: italic; } .calc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin-top: 0.75rem; } .calc-grid-item { padding: 0.6rem 0.8rem; background: #f8f9fa; border-radius: 4px; border: 1px solid #eee; } .calc-grid-item .label { font-size: 0.75rem; color: #888; text-transform: uppercase; letter-spacing: 0.04em; } .calc-grid-item .value { font-size: 1.1rem; font-weight: 600; color: #1a1a1a; } @media (max-width: 720px) { .calc-row { flex-direction: column; align-items: flex-start; gap: 0.3rem; } .calc-row label { min-width: auto; } .calc-row input[type="number"], .calc-row select { max-width: 100%; width: 100%; } .calc-grid { grid-template-columns: 1fr; } } .calc-chart { margin: 1rem 0; text-align: center; } .calc-chart svg { max-width: 100%; height: auto; } .calc-chart-legend { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.6rem 1.2rem; margin-top: 0.6rem; font-size: 0.8rem; color: #555; } .calc-chart-legend span { display: inline-flex; align-items: center; gap: 0.3rem; } .calc-chart-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; font-style: normal; } .calc-related { max-width: 640px; margin: 2rem 0 1rem; padding: 1.25rem 1.5rem; background: #f8f9fa; border: 1px solid #e8e8e8; border-radius: 8px; } .calc-related h3 { font-family: Georgia, serif; font-size: 1rem; color: #1a1a1a; margin: 0 0 0.75rem; padding-bottom: 0.4rem; border-bottom: 2px solid var(--ac, #3d5a80); } .calc-related-list { list-style: none; padding: 0; margin: 0 0 0.75rem; display: grid; grid-template-columns: 1fr 1fr; gap: 0.4rem 1.5rem; } .calc-related-list li a { font-size: 0.88rem; color: var(--ac, #3d5a80); text-decoration: none; } .calc-related-list li a:hover { text-decoration: underline; } .calc-browse-all { margin: 0.5rem 0 0; font-size: 0.9rem; font-weight: 600; } .calc-browse-all a { color: var(--ac, #3d5a80); text-decoration: none; } .calc-browse-all a:hover { text-decoration: underline; } @media (max-width: 720px) { .calc-related-list { grid-template-columns: 1fr; } }

Subnet Mask / CIDR Calculator

Enter an IPv4 address and a CIDR prefix length (0–32) to calculate the subnet mask, network address, broadcast address, usable host range, and total number of usable hosts.

IPv4 Address

CIDR Prefix Length (0–32)

Calculate Results will appear here.

function subCalc() { const resultDiv = document.getElementById("sub-result");

// --- Read inputs --- const ipRaw = document.getElementById("sub-ip").value.trim(); const cidrRaw = document.getElementById("sub-cidr").value.trim();

// --- Validate CIDR --- const cidr = parseInt(cidrRaw, 10); if (cidrRaw === "" || isNaN(cidr) || cidr 32) { resultDiv.innerHTML = "⚠ Please enter a valid CIDR prefix length between 0 and 32."; return; }

// --- Validate IP --- const octets = ipRaw.split("."); if (octets.length !== 4) { resultDiv.innerHTML = "⚠ Please enter a valid IPv4 address (e.g. 192.168.1.100)."; return; } const octetNums = octets.map(o => parseInt(o, 10)); if (octetNums.some(o => isNaN(o) || o 255)) { resultDiv.innerHTML = "⚠ Each octet must be an integer between 0 and 255."; return; }

// --- Core calculations --- // Convert IP to 32-bit unsigned integer const ipInt = ((octetNums[0] >> 0;

// Subnet mask: cidr leading 1-bits, rest 0-bits // For cidr=0 the mask is 0x00000000; for cidr=32 it is 0xFFFFFFFF const maskInt = cidr === 0 ? 0 : (0xFFFFFFFF >> 0;

// Network address: IP AND mask const networkInt = (ipInt & maskInt) >>> 0;

// Broadcast address: network OR (NOT mask) const broadcastInt = (networkInt | (~maskInt >>> 0)) >>> 0;

// Usable hosts // /31 and /32 are special cases (RFC 3021 / loopback) let firstHostInt, lastHostInt, numHosts; if (cidr === 32) { firstHostInt = networkInt; lastHostInt = networkInt; numHosts = 1; } else if (cidr === 31) { firstHostInt = networkInt; lastHostInt = broadcastInt; numHosts = 2; } else { firstHostInt = (networkInt + 1) >>> 0; lastHostInt = (broadcastInt - 1) >>> 0; numHosts = Math.pow(2, 32 - cidr) - 2; }

// Helper: int → dotted-decimal function intToIP(n) { return [ (n >>> 24) & 0xFF, (n >>> 16) & 0xFF, (n >>> 8) & 0xFF, n & 0xFF ].join("."); }

// Helper: int → binary string with dots every 8 bits function intToBin(n) { return ("00000000000000000000000000000000" + (n >>> 0).toString(2)) .slice(-32) .match(/.{8}/g).join("."); }

// Wildcard mask (inverse mask) const wildcardInt = (~maskInt) >>> 0;

// IP class (classful, informational only)
const firstOctet = octetNums[0];
let ipClass;
if (firstOctet = 16 && octetNums[1] = 31
? (cidr === 32 ? "1 (host route)" : "2 (point-to-point, RFC 3021)")
numHosts.toLocaleString();

resultDiv.innerHTML = `

Field Value Binary

IP Address ${intToIP(ipInt)} ${intToBin(ipInt)}

Subnet Mask ${intToIP(maskInt)} ${intToBin(maskInt)}

Wildcard Mask ${intToIP(wildcardInt)} ${intToBin(wildcardInt)}

Network Address ${intToIP(networkInt)} / ${cidr} ${intToBin(networkInt)}

Broadcast Address ${intToIP(broadcastInt)} ${intToBin(broadcastInt)}

First Usable Host ${intToIP(firstHostInt)} ${intToBin(firstHostInt)}

Last Usable Host ${intToIP(lastHostInt)} ${intToBin(lastHostInt)}

Usable Hosts ${hostsLabel}

Total Addresses ${totalAddresses.toLocaleString()} (2${32 - cidr})

IP Class Class ${ipClass}  |  ${isPrivate ? "🔒 Private (RFC 1918)" : "🌐 Public"}

`; }

#### Formulas

Subnet Mask — A 32-bit mask with the first n bits set to 1 (network portion) and the remaining 32 − n bits set to 0 (host portion):

mask = (0xFFFFFFFF << (32 − n)) & 0xFFFFFFFF

Network Address — Bitwise AND of the IP address and the subnet mask:

network = IP & mask

Broadcast Address — Network address OR the bitwise complement (wildcard) of the mask:

broadcast = network | (~mask & 0xFFFFFFFF)

Wildcard Mask — Bitwise complement of the subnet mask:

wildcard = ~mask & 0xFFFFFFFF

Usable Hosts — Total addresses minus network and broadcast addresses (for /0–/30):

hosts = 2(32 − n) − 2

Special cases: /31 = 2 hosts (RFC 3021 point-to-point links); /32 = 1 host route.

#### Assumptions & References

More Calculators

Read Next

Study Time Planner ANA › Life Services Authority › National Calculator Authority › Study Time Planner .calc-container { max-width: 640px; margin:...

References