Skip to content
Back to blog
Field notes · dev

Your Subnet Calculator Says /31 Has 0 Usable Hosts. RFC 3021 Says 2.

/31 has 2 usable hosts (RFC 3021), /32 has 1, IPv6 has no broadcast, and IPv6 point-to-point uses /127. Four subnet calculator errors, checked against the RFCs.

6 min readSimon Wong

Type 10.0.0.0/31 into the first three subnet calculators that come up on Google. All three report 0 usable hosts. That answer was wrong in December 2000, when RFC 3021 defined /31 as a 2-address point-to-point link with both addresses usable. The "network" and "broadcast" semantics simply don't apply at a 2-address allocation. The RFC has been a Standards Track document for 25 years; Cisco IOS has shipped support since 12.2; Juniper, Arista, Linux, FreeBSD, and OpenBSD all honor it. Calculators that still subtract 2 from a 2-address subnet are reporting 2 - 2 = 0 instead of 2.

/32 has the same problem in a different costume. The textbook formula 2^(32-prefix) - 2 returns -1 for /32. Some calculators print -1, some clamp to 0, and most never tell the user that a /32 is a perfectly valid host route — the way every BGP loopback, every Kubernetes pod IP, every ip route add 192.0.2.5/32 dev lo works in production. A /32 has exactly 1 usable address, full stop.

This tool computes:

prefix = 31 → usable = 2   (RFC 3021 point-to-point)
prefix = 32 → usable = 1   (host route)
prefix ≤ 30 → usable = 2^(32-prefix) - 2

That's three lines and it matches every modern router. Most calculators ship the third line as if it were the only line.

IPv6 Has No Broadcast. Stop Printing One.

Open a popular IPv6 subnet calculator and you'll see a "Broadcast Address" field. RFC 4291 §2.1: "There are no broadcast addresses in IPv6, their function being superseded by multicast addresses." The "broadcast" field in those calculators is just the all-ones host portion of the prefix — which in IPv6 is a perfectly valid unicast address that a host can sit on. Printing it as "broadcast" teaches a generation of operators to leave one address unused for no reason.

The same family of calculators recommends /127 only as a footnote and still suggests /126 or even /64 for router-to-router links. RFC 6164 made /127 the standard for IPv6 inter-router point-to-point links in 2011, specifically to avoid the ping-pong attack against /64 links. This calculator labels /127 as "IPv6 point-to-point per RFC 6164" and never prints a broadcast field. The IPv6 result type has no broadcast property at all — it can't be rendered by accident.

JavaScript Bitwise Ops Wrap at 2³¹. Most Subnet Code Hits This.

A /8 IPv4 subnet has 2^24 = 16,777,216 addresses. A /0 has 2^32 = 4,294,967,296. Both routinely show up in VLSM planning against carrier-grade aggregates. Almost every subnet-calculator implementation on GitHub computes the subnet size with 1 << (32 - prefix). In JavaScript, bitwise operators coerce to signed int32:

1 << 32                       // → 1   (not 4,294,967,296)
1 << 31                       // → -2147483648 (signed)
(1 << 31) >>> 0               // → 2147483648 (unsigned, the right number)
Math.pow(2, 32)               // → 4294967296 ✓

1 << 32 is 1, not 4,294,967,296, because the shift count gets masked to 5 bits (32 & 31 = 0). VLSM allocation rooted at 0.0.0.0/0 and asked for any subnet larger than /2 produces silent garbage with the bitwise form. This tool uses Math.pow(2, 32 - prefix) and >>> 0 for the final coercion to unsigned — slower by maybe 3 nanoseconds per call, correct for the entire IPv4 space.

The VLSM allocator hits the same trap in a sneakier place. To align a cursor up to a power-of-two boundary, the conventional trick is:

const aligned = (cursor + size - 1) & ~(size - 1);   // breaks above 2^31

For a /8 parent (size up to 2^24) the cursor can easily exceed 2^31 mid-allocation. The bitwise AND wraps to a negative int32. The replacement:

const aligned = Math.ceil(cursor / size) * size;     // works through 2^53

is half a CPU cycle slower and stays correct across the entire IPv4 range. The cost of "fast" bitwise alignment is a silent off-by-billions bug. The cost of Math.ceil is invisible to a human.

First-Fit Decreasing for VLSM, Math.ceil for Alignment

The VLSM mode takes a parent CIDR (say 10.0.0.0/22, 1,024 addresses) and a list of named subnet requirements (HQ: 200, Branch: 100, Lab: 50, P2P: 2). The classic greedy algorithm is First-Fit Decreasing: sort requests by required size descending, then allocate each at the next correctly-aligned boundary. For each requirement, the prefix is picked as the smallest one that fits hosts + 2 net/broadcast overhead — except for the /31 and /32 special cases, where 2 hosts → /31 and 1 host → /32 directly.

The packing is provably optimal for power-of-two allocations because any subnet of size 2^k must start at an address that is a multiple of 2^k. Sorting largest-first eliminates the fragmentation that would occur if you placed a /30 before a /24 (the /24 would have to skip past the /30's alignment hole). With this ordering, every requirement either fits at the cursor or fits at the next 2^k boundary, never any further. The implementation runs O(n log n) for the sort plus O(n²) for a defensive pairwise overlap check — both finish in microseconds for the 64-row cap.

Aggregation: XOR + Bit Length Beats Trial-Subdivision

Given 192.0.2.0/24, 192.0.2.128/25, and 192.0.3.0/24, what's the smallest CIDR covering all three? The shortcut is one XOR and one bit-length call:

const xor = minAddress ^ maxAddress;        // bigint for IPv6 reach
const commonPrefix = totalBits - bitLength(xor);

The common prefix is exactly the number of leading bits where min and max agree. XOR clears every agreeing bit and leaves every disagreeing bit set, so the bit length of the XOR is the number of trailing bits where they disagree. Subtracting from 32 (or 128 for IPv6) gives the prefix. For the example above, min = 192.0.2.0, max = 192.0.3.255, min XOR max = 0.0.1.255 (bit length 9), common prefix = 32 − 9 = 23, aggregate = 192.0.2.0/23 covering all four octets and 512 addresses. Trial-subdivision implementations loop from /32 down to /0 checking containment at each step; the XOR form is one operation regardless of input size, and works identically for IPv4 (cast to BigInt) and IPv6 (native BigInt).

The "gaps" number printed alongside the aggregate — aggregate_size − merged_covered — is what tells you whether the aggregate actually summarizes your inputs (0 gaps = perfectly contiguous) or whether you're announcing addresses you don't own (gaps > 0 = the aggregate covers holes). BGP route optimizers print this. Most calculators don't.

Try It

Open the IP Subnet Calculator.

  • IPv4 tab: enter 10.0.0.0/31. The result reads 2 usable hosts (RFC 3021 point-to-point), no broadcast field.
  • IPv4 tab: enter 192.0.2.5/32. The result reads 1 usable host (host route), no broadcast field.
  • IPv6 tab: enter 2001:db8::/127. The result reads 2 addresses (RFC 6164 point-to-point). There is no broadcast field anywhere on the panel.
  • VLSM tab: parent 10.0.0.0/22, requirements HQ 200, Branch 100, Lab 50, P2P 2. The allocator returns HQ 10.0.0.0/24, Branch 10.0.1.0/25, Lab 10.0.1.128/26, P2P 10.0.1.192/31 with 254 addresses of leftover capacity.
  • Aggregate tab: paste 192.0.2.0/24, 192.0.2.128/25, 192.0.3.0/24. The aggregate reads 192.0.2.0/23 with 0 gaps — perfectly contiguous.

Three of the five inputs above are answered incorrectly by the top Google results for "subnet calculator" as of this writing.