Networking

Subnet Calculator CIDR: Hosts, Ranges and Masks by Hand

A subnet calculator for CIDR takes a block such as 172.20.37.200/21 and returns its network address, broadcast address, usable host range and host count. You can do the same arithmetic by hand with one formula, usable hosts = 2(32 − prefix) − 2, and one shortcut for finding where the block starts.

This article shows the by-hand method, a few lines of Python that act as your own calculator, and how to pick a prefix when you know how many devices you need. For what the slash number means in the first place, see CIDR notation explained; for the dotted mask side, see subnet mask explained.

What a subnet calculator gives you

Every CIDR calculator, whatever its interface, returns the same handful of values for an input block:

  • Network address: the first address, with all host bits set to 0.
  • Broadcast address: the last address, with all host bits set to 1.
  • Usable host range: everything between those two.
  • Subnet mask: the prefix written as four octets, for example 255.255.248.0 for /21.
  • Total addresses and usable hosts: 2(32 − prefix), and that number minus 2.

RFC 4632 defines the prefix as a decimal value from 0 to 32 giving the number of significant bits. Everything else follows from it.

Step 1: count the host bits and the hosts

Host bits = 32 − prefix. Total addresses = 2host bits. Usable hosts = total − 2, because the network and broadcast addresses normally can't be given to a device. This table covers every size you are likely to need:

PrefixHost bitsTotal addressesUsable hosts
/30242
/29386
/2841614
/2753230
/2666462
/257128126
/248256254
/239512510
/22101,0241,022
/21112,0482,046
/20124,0964,094

Two exceptions to "subtract 2". A /32 is one address and one host. A /31 has two addresses, and RFC 3021 says that on a point-to-point link with a 31-bit mask, both addresses "MUST be interpreted as host addresses", so both are usable.

Step 2: find the network and broadcast address

Only one octet changes meaning: the first one that isn't fully 255 in the mask. Call it the interesting octet. Then:

  1. Block size = 256 − the mask's value in the interesting octet.
  2. The network address is the largest multiple of the block size that is not above the address's value in that octet. Octets to the left stay as they are; octets to the right become 0.
  3. The broadcast address is the network address plus the block size, minus 1 in that octet. Octets to the right become 255.

Worked example for 172.20.37.200/21:

  • /21 = 8 + 8 + 5 bits, so the mask is 255.255.248.0 and the interesting octet is the third.
  • Block size = 256 − 248 = 8.
  • The third octet is 37. The largest multiple of 8 not above 37 is 32. Network address: 172.20.32.0.
  • Broadcast: the block covers third-octet values 32 to 39, so the broadcast address is 172.20.39.255.
  • Usable range: 172.20.32.1 to 172.20.39.254.
  • Host count: 211 − 2 = 2,046.

Notice that the address you typed, 172.20.37.200, is just one host somewhere inside the block. People often paste a host address with a prefix and expect it to be the network address. It isn't, and some tools will refuse it (see the Python section).

When the prefix falls on an octet boundary there is nothing to calculate. 10.4.0.0/16 runs from 10.4.0.0 to 10.4.255.255.

Calculate it with Python

Python's standard library ipaddress module is a complete subnet calculator, with no install needed. This one-liner reproduces the worked example above:

python -c "import ipaddress as i; n=i.ip_interface('172.20.37.200/21').network; h=list(n.hosts()); print(n, n.netmask, n.broadcast_address, len(h), h[0], h[-1])"

Example output:

172.20.32.0/21 255.255.248.0 172.20.39.255 2046 172.20.32.1 172.20.39.254

That matches the hand calculation. Three details from the Python documentation are worth knowing:

  • hosts() returns every address except the network and broadcast addresses. For a /31 it returns both addresses, and for a /32 it returns the single address, which agrees with RFC 3021 and the table above.
  • ip_interface() accepts a host address with a prefix and exposes .network. ip_network() on the same text raises ValueError: 172.20.37.200/21 has host bits set unless you pass strict=False.
  • num_addresses is the total count, so usable hosts for ordinary LAN blocks is num_addresses - 2.

To split a block into equal smaller blocks, use subnets(). For example, list(ipaddress.ip_network('192.168.10.0/24').subnets(new_prefix=26)) returns four /26 networks starting at 192.168.10.0, .64, .128 and .192. The reverse is supernet(), which returns the larger block that contains a network.

How to pick a prefix for a number of hosts

Going the other way, start with the host count and find the smallest block that fits. Find the smallest number of host bits h where 2h − 2 is at least the hosts you need, then the prefix is 32 − h.

Devices neededHost bitsPrefixUsable hosts
2 (a link between two routers)2/302
255/2730
506/2662
1007/25126
5009/23510

Leave room to grow. A subnet sized exactly for 100 devices has to be renumbered the day device 127 arrives, and a /25 holds only 126.

Example: carving one /24 into differently sized subnets

Suppose you have 192.168.10.0/24 and need subnets for 100 devices, 50 devices, 25 devices and one router link. Place the largest block first so every block starts on a boundary that is a multiple of its own size. This is called variable-length subnet masking (VLSM), and it is allowed because CIDR lets prefixes be any length.

NeedSubnetUsable rangeBroadcast
100 devices192.168.10.0/25.1 to .126192.168.10.127
50 devices192.168.10.128/26.129 to .190192.168.10.191
25 devices192.168.10.192/27.193 to .222192.168.10.223
Router link192.168.10.224/30.225 to .226192.168.10.227

Everything from 192.168.10.228 to 192.168.10.255 is still unallocated. If you had placed the /30 first and the /25 afterwards, the /25 could not start at .4: a /25 must begin at .0 or .128, so you would waste space or run out. Largest first avoids that. The blocks never overlap, which matters because overlapping subnets make routing ambiguous.

Cloud subnets reserve more addresses

The "minus 2" rule is the classic LAN rule. Cloud providers can reserve more. AWS documents that in every IPv4 subnet of a VPC, the first four addresses and the last address are not available for your use. In 10.0.0.0/24 those are 10.0.0.0 (network), 10.0.0.1 (VPC router), 10.0.0.2 (DNS), 10.0.0.3 (reserved for future use) and 10.0.0.255 (broadcast, which AWS reserves although a VPC doesn't support broadcast). A /24 subnet there gives 251 usable addresses, not 254.

AWS also limits subnet sizes to between /28 and /16. A /28 has 16 addresses, so 11 are usable in a VPC. As of November 2026 this is what AWS's documentation states; other providers have their own reservations, so check your provider's page before sizing a subnet tightly.

If your calculator and your router disagree

  • Host bits set. You entered a host address (.200) and expected a network address. Compute the network address with Step 2.
  • Misaligned block. A /26 can only start at .0, .64, .128 or .192. If your "network" starts anywhere else, the prefix or the start address is wrong.
  • Usable count is off by 2 or 3. You are on a /31, a /32, a cloud subnet with extra reserved addresses, or a device that treats the first address as special. Look at the platform's documentation, not the calculator.
  • Prefix too small. Counting 2(32 − prefix) works only when the prefix is written correctly. /8 is 16,777,216 addresses and /80 isn't valid for IPv4 at all.

For the idea behind the whole exercise, see what is a subnet. The calculation is the same on any network; only the reserved addresses change.

Frequently asked questions

How do I calculate the number of hosts in a CIDR block?
Subtract the prefix length from 32 to get the host bits, raise 2 to that power, and subtract 2 for the network and broadcast addresses. A /27 has 32 - 27 = 5 host bits, so 2^5 = 32 addresses and 30 usable hosts. The exceptions are /31 and /32, where nothing is subtracted.
What is the first and last usable address in a subnet?
The first usable address is the network address plus one, and the last is the broadcast address minus one. In 192.168.10.128/26 the network address is 192.168.10.128, the broadcast address is 192.168.10.191, so usable hosts run from 192.168.10.129 to 192.168.10.190.
Why does AWS give me fewer usable addresses than a subnet calculator?
AWS reserves the first four and the last address of every IPv4 subnet in a VPC, so five addresses are unavailable instead of the usual two. A /24 subnet in a VPC therefore has 251 usable addresses, not 254.
Can I use a subnet calculator for IPv6?
The same prefix logic applies, but host counts are rarely the goal. IPv6 LAN subnets are normally /64, so you plan by how many subnets you need, not how many hosts fit in each.

Sources