CIDR / Subnet Calculator — Free HTML CSS JS Snippet
CIDR / Subnet Calculator · Dev · Plain HTML, CSS & JS · Live preview
What's included
Features
About this UI Snippet
CIDR / Subnet Calculator — Network, Broadcast & Host Range from IPv4 CIDR Notation via Bitwise Arithmetic

CIDR (Classless Inter-Domain Routing) notation packs an IPv4 address and a network prefix length into one compact string, like 192.168.1.10/24. Reading the actual network boundaries out of that string by hand means converting each octet to binary and applying a mask — this snippet does that arithmetic for real, with genuine bitwise operations on 32-bit integers, not a lookup table of common prefixes.
Packing an IPv4 address into a single 32-bit integer
ipToInt() takes the four octet numbers and combines them with bit shifts: (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3], then coerces the result to an unsigned 32-bit integer with >>> 0 (a zero-fill right shift by zero, a common JavaScript idiom for forcing a value into unsigned 32-bit range). This matters because JavaScript's bitwise operators work on signed 32-bit integers internally — without the >>> 0 coercion, an address with a high first octet like 192.x.x.x would compute to a negative number and every downstream calculation would be wrong. intToIp() reverses the packing, extracting each octet with a right-shift and an & 255 mask.
Deriving the subnet mask from the prefix length
The subnet mask for a given prefix /n is n set bits followed by 32-n zero bits. The calculator builds it with (0xFFFFFFFF << (32 - prefix)) >>> 0 — starting from all 32 bits set, then shifting left by the host-bit count zeroes out exactly that many low bits. A prefix of /0 is special-cased to a mask of 0, since shifting a 32-bit value left by 32 is undefined behavior in JavaScript (shift amounts wrap modulo 32, so << 32 is actually << 0, which would incorrectly leave all bits set).
Network and broadcast addresses via AND and OR with the wildcard mask
The network address is ipInt & maskInt — ANDing zeroes out every host bit, leaving only the network portion. The broadcast address is the network address ORed with the wildcard mask (~maskInt >>> 0, the bitwise complement of the subnet mask), which sets every host bit to 1. This is exactly the arithmetic a router or a language like Python's ipaddress module performs internally; the calculator just makes each step and its rationale visible.
Usable host count and the /31, /32 special cases
Total addresses in a subnet is 2^(32-prefix). Usable hosts is normally that total minus 2, because the network and broadcast addresses at the two ends of the range can't be assigned to a host. Two edge cases are called out explicitly rather than silently applying the generic formula: a /32 is a single-host route with no network/broadcast distinction at all, and a /31 (per RFC 3021) is a point-to-point link where *both* addresses in the two-address block are usable, since there's no room to spare a broadcast address on a link with only two possible endpoints.
The binary bit visualization
renderBinary() converts each octet to an 8-character binary string and colors the first prefix bits (the network portion, shared by every host in the subnet) differently from the remaining host bits, letting you see directly which bits the mask fixes and which bits are free to vary across every address in the range — the same visual most networking textbooks draw by hand.
Build with AI
Build, Understand, Optimize, and Extend It With AI
Give this snippet's JavaScript to an AI assistant like Claude and ask it to explain step by step why the >>> 0 coercion is necessary after every bitwise operation here — it's a subtlety that trips up most hand-written IP address arithmetic in JavaScript. It's also a solid base to extend: ask for IPv6 support using BigInt for 128-bit math, a subnet-splitting feature that divides a given CIDR block into N equal smaller subnets, or a reverse mode that takes two IP addresses and computes the smallest CIDR block containing both.
Prompt to recreate it
Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:
Build a CIDR / subnet calculator in plain HTML, CSS, and JavaScript, no libraries.
Requirements:
- A single text input accepting IPv4 CIDR notation like 192.168.1.10/24, recalculating live on every keystroke.
- Validate the IP address (four octets, each 0-255) and the prefix length (0-32) separately, showing a specific inline error message for each kind of invalid input.
- Pack the IPv4 address into a 32-bit integer using bit shifts, being careful to coerce results to unsigned 32-bit range (JavaScript's bitwise operators are signed by default) so addresses with a high first octet compute correctly.
- Compute and display: subnet mask, wildcard mask (bitwise complement of the subnet mask), network address (address AND mask), broadcast address (network address OR wildcard mask), total addresses (2 to the power of host bits), and the first/last usable host addresses.
- Correctly special-case /31 (RFC 3021 point-to-point link, both addresses usable, no broadcast) and /32 (single host, no network/broadcast distinction) rather than applying the generic "total minus 2" formula to every prefix length.
- Render a binary bit visualization of the address's 32 bits, coloring the network-portion bits (determined by the prefix length) differently from the host-portion bits.Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.
Step by step
How to Use
- 1Type an address in CIDR notationEnter an IPv4 address and prefix length together, e.g. 10.0.0.0/16 or 192.168.1.10/24 — results recalculate on every keystroke.
- 2Read the network and broadcast addressesThese two accented cells show the exact boundaries of the subnet that address belongs to.
- 3Check the subnet and wildcard masksThe wildcard mask (the bitwise inverse of the subnet mask) is what many router ACL configurations expect instead of the subnet mask itself.
- 4Find the usable host rangeFirst and last usable host are the network and broadcast addresses plus/minus one, with /31 and /32 handled as their documented special cases.
- 5Compare total addresses vs usable hostsTotal addresses is 2 to the power of the host-bit count; usable hosts subtracts the two reserved addresses at typical prefix lengths.
- 6Study the binary bit breakdownThe colored bit row shows exactly which leading bits the prefix length fixes as the network portion versus which trailing bits vary per host.
Real-world uses
Common Use Cases
Got questions?
Frequently Asked Questions
JavaScript's bitwise operators treat numbers as signed 32-bit integers. An IPv4 address with a high first octet (anything 128 and above) produces a negative number when packed into a 32-bit integer using plain arithmetic. The >>> 0 idiom (zero-fill right shift by zero) forces the value back into the unsigned 0 to 4294967295 range so address comparisons and formatting work correctly.
A /32 identifies exactly one address with no room for a separate network or broadcast address, so there is no usable host range at all. A /31, per RFC 3021, is reserved for point-to-point links: rather than wasting one of only two available addresses on a broadcast address, both addresses in the block are treated as usable host addresses.
The wildcard mask is the bitwise complement (NOT) of the subnet mask — where the subnet mask has 1s for the network portion, the wildcard mask has 1s for the host portion. Some router platforms, particularly Cisco access control lists, expect the wildcard mask form instead of the standard subnet mask when specifying an address range.
The input IP address is converted to a 32-bit integer, then bitwise-ANDed with the subnet mask. ANDing zeroes out every bit in the host portion while leaving the network portion bits unchanged, which is exactly what "the network this address belongs to" means at the bit level.
The calculator validates every octet is an integer between 0 and 255 before doing any math, and shows a clear inline error instead of silently computing with an invalid value.
No, this calculator is IPv4-only — it packs addresses into a single 32-bit integer, which doesn't apply to IPv6's 128-bit address space. An IPv6 subnet calculator would need a different underlying representation, typically arrays of 16-bit groups or BigInt-based 128-bit arithmetic.