Calculator for Subnet Mask

Subnet Mask Calculator

Subnet Details:

Enter an IP address and CIDR prefix to see the subnet details.

function calculateSubnet() { var ipAddressInput = document.getElementById("ipAddress").value; var cidrPrefixInput = document.getElementById("cidrPrefix").value; // Validate IP Address var ipParts = ipAddressInput.split('.'); if (ipParts.length !== 4) { document.getElementById("subnetResult").innerHTML = "Invalid IP Address format. Please use X.X.X.X"; return; } for (var i = 0; i < 4; i++) { var part = parseInt(ipParts[i], 10); if (isNaN(part) || part 255) { document.getElementById("subnetResult").innerHTML = "Invalid IP Address octet. Each part must be between 0 and 255."; return; } } // Validate CIDR Prefix var cidr = parseInt(cidrPrefixInput, 10); if (isNaN(cidr) || cidr 32) { document.getElementById("subnetResult").innerHTML = "Invalid CIDR Prefix. Must be a number between 0 and 32."; return; } // Convert IP to 32-bit integer var ipInt = (parseInt(ipParts[0], 10) << 24) | (parseInt(ipParts[1], 10) << 16) | (parseInt(ipParts[2], 10) << 8) | (parseInt(ipParts[3], 10)); // Calculate Subnet Mask (32-bit integer) // Using -1 (all bits set to 1) and left shifting to create the mask var subnetMaskInt = -1 <>> 24) & 0xFF, (subnetMaskInt >>> 16) & 0xFF, (subnetMaskInt >>> 8) & 0xFF, subnetMaskInt & 0xFF ].join('.'); // Calculate Network Address (32-bit integer) var networkAddressInt = ipInt & subnetMaskInt; // Convert Network Address to dotted decimal var networkAddress = [ (networkAddressInt >>> 24) & 0xFF, (networkAddressInt >>> 16) & 0xFF, (networkAddressInt >>> 8) & 0xFF, networkAddressInt & 0xFF ].join('.'); // Calculate Broadcast Address (32-bit integer) // OR with the inverted subnet mask (host bits set to 1) var broadcastAddressInt = networkAddressInt | (~subnetMaskInt); // Convert Broadcast Address to dotted decimal var broadcastAddress = [ (broadcastAddressInt >>> 24) & 0xFF, (broadcastAddressInt >>> 16) & 0xFF, (broadcastAddressInt >>> 8) & 0xFF, broadcastAddressInt & 0xFF ].join('.'); // Calculate First Usable Host, Last Usable Host, and Total Usable Hosts var firstUsableHost = "N/A"; var lastUsableHost = "N/A"; var totalUsableHosts = 0; if (cidr >> 24) & 0xFF, (firstUsableHostInt >>> 16) & 0xFF, (firstUsableHostInt >>> 8) & 0xFF, firstUsableHostInt & 0xFF ].join('.'); var lastUsableHostInt = broadcastAddressInt – 1; lastUsableHost = [ (lastUsableHostInt >>> 24) & 0xFF, (lastUsableHostInt >>> 16) & 0xFF, (lastUsableHostInt >>> 8) & 0xFF, lastUsableHostInt & 0xFF ].join('.'); totalUsableHosts = Math.pow(2, (32 – cidr)) – 2; } else if (cidr === 31) { // Point-to-point links, 2 addresses, 0 usable hosts totalUsableHosts = 0; // First/Last usable hosts are N/A as per common interpretation for general purpose use } else if (cidr === 32) { // Single host address, 0 usable hosts totalUsableHosts = 0; // First/Last usable hosts are N/A as per common interpretation } // Display results var resultHTML = "

Subnet Details:

"; resultHTML += "IP Address: " + ipAddressInput + ""; resultHTML += "CIDR Prefix: /" + cidr + ""; resultHTML += "Subnet Mask: " + subnetMask + ""; resultHTML += "Network Address: " + networkAddress + ""; resultHTML += "Broadcast Address: " + broadcastAddress + ""; resultHTML += "First Usable Host: " + firstUsableHost + ""; resultHTML += "Last Usable Host: " + lastUsableHost + ""; resultHTML += "Total Usable Hosts: " + totalUsableHosts + ""; document.getElementById("subnetResult").innerHTML = resultHTML; }

Understanding the Subnet Mask Calculator

A subnet mask is a 32-bit number that separates the IP address into the network address and the host address. In simpler terms, it tells a computer which part of an IP address refers to the network it's on, and which part refers to the specific device (host) within that network.

What is an IP Address?

An IP (Internet Protocol) address is a unique numerical label assigned to each device connected to a computer network that uses the Internet Protocol for communication. IPv4 addresses are typically represented in dotted-decimal notation (e.g., 192.168.1.10), consisting of four numbers (octets) separated by dots, where each number ranges from 0 to 255.

The Role of the Subnet Mask

When an IP address is combined with a subnet mask, it allows a network device to determine if another IP address is on the same local network or on a remote network. This is crucial for routing traffic efficiently. The subnet mask works by performing a bitwise AND operation with the IP address. The bits that are '1' in the subnet mask correspond to the network portion of the IP address, while the bits that are '0' correspond to the host portion.

CIDR Notation

CIDR (Classless Inter-Domain Routing) is a method for allocating IP addresses and routing IP packets. It's a more flexible alternative to the old class-based IP addressing system. Instead of using predefined classes (A, B, C), CIDR uses a "prefix length" to indicate the number of bits in the IP address that represent the network portion. This prefix length is appended to the IP address with a slash, like /24. For example, 192.168.1.0/24 means that the first 24 bits of the IP address represent the network, and the remaining 8 bits represent the hosts within that network.

Key Subnet Details Explained:

  • IP Address: The specific address of a device on the network.
  • CIDR Prefix: The number of bits used for the network portion of the IP address.
  • Subnet Mask: The 32-bit mask in dotted-decimal format that defines the network and host portions. For a /24 CIDR, the subnet mask is 255.255.255.0.
  • Network Address: The first address in a subnet. It's used to identify the network itself and cannot be assigned to a host. All host bits are 0.
  • Broadcast Address: The last address in a subnet. It's used to send data to all devices within that specific subnet and cannot be assigned to a host. All host bits are 1.
  • First Usable Host: The first IP address in the subnet that can be assigned to a device. This is typically the Network Address + 1.
  • Last Usable Host: The last IP address in the subnet that can be assigned to a device. This is typically the Broadcast Address – 1.
  • Total Usable Hosts: The total number of IP addresses available for devices within the subnet. This is calculated as 2^(32 - CIDR prefix) - 2 (subtracting the network and broadcast addresses). For /31 and /32 subnets, there are no usable hosts in the traditional sense.

Example Usage:

Let's say you have an IP address 192.168.1.10 and a CIDR prefix of 24:

  • IP Address: 192.168.1.10
  • CIDR Prefix: /24
  • Subnet Mask: 255.255.255.0
  • Network Address: 192.168.1.0
  • Broadcast Address: 192.168.1.255
  • First Usable Host: 192.168.1.1
  • Last Usable Host: 192.168.1.254
  • Total Usable Hosts: 254

This calculator helps you quickly determine these critical network parameters, which are essential for network design, troubleshooting, and security.

Leave a Comment