Skip to main content
Networking
Published 7 min read

How to Calculate the Number of IP Addresses in a CIDR Block

One formula answers this for any prefix length. Here's the formula, plus worked examples for the sizes you'll actually use.

This comes up constantly when sizing infrastructure: given a prefix length, how many IP addresses does that actually give you? The formula is simple once you have it, and it applies to any prefix from /0 to /32.

The formula

Total addresses = 2^(32 - prefix length)
Usable hosts = Total addresses - 2   (for most prefixes - see the /31 and /32 notes below)

IPv4 addresses are 32 bits. Subtracting the prefix length from 32 tells you how many bits are left for host addresses, and 2 raised to that power gives you the total address count for the block.

Worked examples

PrefixHost bits (32 - prefix)Total addresses (2^host bits)Usable hosts
/248256254
/257128126
/2666462
/2753230
/2841614
/29386
/30242

Step-by-step: how many addresses in a /27?

  1. Host bits = 32 - 27 = 5
  2. Total addresses = 2^5 = 32
  3. Usable hosts = 32 - 2 = 30

The exceptions: /31 and /32

The "subtract 2" rule has two documented exceptions:

  • /31 has exactly 2 addresses, and per RFC 3021, both are usable - this is a special case designed for point-to-point links where there's no need for a separate network or broadcast address.
  • /32 identifies a single specific address with no network or broadcast concept at all - it's used for host routes, not a subnet in the traditional sense.

Sizing a subnet for a known device count

This formula also works in reverse when you know how many devices you need to support. To fit 40 devices: 2^n - 2 must be at least 40. 2^5 - 2 = 30 (too small), 2^6 - 2 = 62 (fits, with room to grow) - so you'd need at least a /26.

The CIDR Calculator does this arithmetic instantly for any prefix, along with the actual network address, broadcast address, and usable range for a real IP block - handy for double-checking before applying a firewall rule or provisioning a subnet.

Common mistakes

  • Forgetting to subtract 2 for the network and broadcast addresses on standard subnets.
  • Applying the "subtract 2" rule to a /31, where it doesn't apply.
  • Off-by-one errors when counting host bits - always subtract the prefix from 32, not from 30 or some other number.

Summary

2^(32 - prefix) gives you the total address count for any CIDR block, and subtracting 2 gives you usable hosts for anything larger than a /31. It's a small formula that covers subnet sizing for essentially every practical network design decision.

Frequently asked questions

How many addresses are in a /24?
256 total addresses (2^8), with 254 usable after the network and broadcast addresses are reserved.
How many usable IPs are in a /26?
A /26 has 64 total addresses (2^6) and 62 usable addresses after reserving the network and broadcast addresses.
Why do you subtract 2 from the total address count?
Every standard subnet reserves its first address (the network address) and its last address (the broadcast address) - neither can be assigned to a host, which is why usable hosts = total addresses minus 2 for most prefixes.

The tool that pairs with this guide.

Did this make subnetting easier?

If these free developer tools save you time, consider buying me a byte. Your support helps keep DevCraftKit free and growing.

Buy me a byte 💾