G-17 · Developer · 5 min read

How to calculate a subnet from CIDR notation

Work out the network, broadcast and usable host range of any IPv4 CIDR block, handle /31 and /32, split a /24 into smaller subnets and spot private ranges.

Updated Uses IP Subnet Calculator
Fig. G-17 · Developer

Subnetting comes up whenever you set up a VPC, write a firewall rule, assign static addresses or read a router configuration. The arithmetic is simple once you see that a prefix length is just a count of fixed bits, but it is easy to slip by one when you do it in your head. This guide explains how CIDR notation works, how to find the network and broadcast addresses by hand, the special cases at /31 and /32, and how to split a network into smaller ones.

Step by step

  1. Open the IP subnet calculator and type an address with its prefix, such as 192.168.1.10/24. You can also enter an address and a netmask (10.0.0.1 255.255.0.0) or a wildcard mask (0.0.0.255) in the optional mask field.
  2. Read the results: network address, broadcast address, first and last usable host, number of usable hosts, netmask and wildcard mask.
  3. Check the address type. The calculator tells you whether the address is private, public, loopback, link-local, carrier-grade NAT, multicast or reserved.
  4. To divide the network, choose Split into and either set a number of subnets or a new prefix length. A table lists each subnet's network, host range and broadcast address.
  5. For IPv6, enter an address such as 2001:db8::1/64 to see it expanded, compressed to its canonical form and reduced to its network prefix.

How CIDR notation works

An IPv4 address is 32 bits, written as four 8-bit octets. In CIDR notation such as 192.168.1.10/24, the number after the slash is the prefix length: how many leading bits identify the network. The remaining bits identify a host on that network.

A /24 fixes the first 24 bits, leaving 8 bits, or 256 addresses, for hosts. The netmask is the same information written as an address: 24 one-bits followed by 8 zero-bits, which is 255.255.255.0. The wildcard mask, used in Cisco ACLs and OSPF, is the inverse: 0.0.0.255.

Within any block, two addresses are normally reserved:

  • The network address, with all host bits set to 0, identifies the subnet itself.
  • The broadcast address, with all host bits set to 1, reaches every host on it.

So the number of usable hosts is 2^(32 − prefix) − 2.

Prefix Netmask Addresses Usable hosts
/32 255.255.255.255 1 1
/31 255.255.255.254 2 2
/30 255.255.255.252 4 2
/29 255.255.255.248 8 6
/28 255.255.255.240 16 14
/27 255.255.255.224 32 30
/26 255.255.255.192 64 62
/25 255.255.255.128 128 126
/24 255.255.255.0 256 254
/23 255.255.254.0 512 510
/22 255.255.252.0 1,024 1,022
/16 255.255.0.0 65,536 65,534

Cloud providers often reserve more than two addresses per subnet. AWS, for example, reserves five in every VPC subnet, so a /24 there has 251 usable addresses. Check your provider's documentation when sizing.

Finding the network and broadcast by hand

When the prefix is a multiple of 8 (/8, /16, /24), the boundary falls between octets and you can read the answer directly. Otherwise, use the block size.

Take 10.20.30.77/27:

  1. The prefix ends in the fourth octet, since 27 is between 24 and 32.
  2. The host bits are 32 − 27 = 5, so the block size is 2^5 = 32.
  3. Subnets in that octet start at multiples of 32: 0, 32, 64, 96 and so on. 77 falls in the block that starts at 64.
  4. The network address is 10.20.30.64 and the broadcast is one less than the next block: 10.20.30.95.
  5. Usable hosts run from 10.20.30.65 to 10.20.30.94, which is 30 addresses.

The same method works for any prefix. For a /20, the boundary is in the third octet with a block size of 2^(24 − 20) = 16, so 172.16.37.9/20 is in 172.16.32.0/20, which runs to 172.16.47.255.

Tip A quick sanity check is that the network address of a block is always divisible by the block size in the octet where the prefix ends. 10.20.30.64/27 is valid because 64 is a multiple of 32. 10.20.30.70/27 is a host address, not a network.

The /31 and /32 special cases

The minus-two rule breaks down for the two smallest prefixes.

  • /31 gives two addresses and, by the old rule, zero usable hosts. RFC 3021 changed that for point-to-point links: with only two ends, a network address and broadcast are unnecessary, so both addresses are usable and there is no broadcast. Most modern routers support /31 links, which saves half the addresses compared with a /30.
  • /32 is a single address. It identifies one host, and is used for loopback interfaces on routers, host routes and single-address firewall rules such as 203.0.113.5/32.

The calculator applies both rules: a /31 shows two usable addresses and no broadcast, and a /32 shows one host.

Splitting a /24 into smaller subnets

Borrowing bits from the host part creates more, smaller subnets. Each bit borrowed doubles the number of subnets and halves their size. To split 192.168.1.0/24 into four, borrow 2 bits, which gives /26 subnets of 64 addresses each:

Subnet Usable hosts Broadcast
192.168.1.0/26 192.168.1.1 – 192.168.1.62 192.168.1.63
192.168.1.64/26 192.168.1.65 – 192.168.1.126 192.168.1.127
192.168.1.128/26 192.168.1.129 – 192.168.1.190 192.168.1.191
192.168.1.192/26 192.168.1.193 – 192.168.1.254 192.168.1.255

If you ask the calculator for a number of subnets that is not a power of two, such as 5, it rounds up to the next power, 8, because equal-sized subnets can only come in powers of two. For subnets of different sizes (variable-length subnet masking), allocate the largest first, then split the remaining space.

Private and special ranges

These IPv4 ranges are not routed on the public internet:

Range Purpose
10.0.0.0/8 Private networks (RFC 1918)
172.16.0.0/12 Private networks (RFC 1918), 172.16.0.0 to 172.31.255.255
192.168.0.0/16 Private networks (RFC 1918)
100.64.0.0/10 Shared address space for carrier-grade NAT (RFC 6598)
127.0.0.0/8 Loopback
169.254.0.0/16 Link-local, used when DHCP fails

A common mistake is treating all of 172.x as private. Only 172.16 to 172.31 are; 172.15.0.1 and 172.32.0.1 are public addresses. When planning a network that may later connect to others through a VPN, avoid the most common choices such as 192.168.0.0/24 and 192.168.1.0/24, since overlapping ranges cannot be routed between sites without NAT.

For converting octets to binary to see the bits, the number base converter is handy. If you are setting file permissions on the servers you are addressing, the chmod calculator covers that side.

FAQ

Common questions.

01How many usable hosts are in a /24?

A /24 has 256 addresses; the first is the network address and the last is the broadcast address. Some cloud providers reserve a few more.

02What is the difference between a netmask and a wildcard mask?

They are inverses. The netmask has 1s for network bits, 255.255.255.0 for a /24; the wildcard mask has 1s for host bits, 0.0.0.255. Wildcard masks appear in Cisco access lists and OSPF configuration.

03Can I use a /31 on an ordinary LAN?

No. RFC 3021 defines /31 for point-to-point links between two devices. A LAN with more than two devices needs a /29 or larger, since a /30 also has only two usable hosts.