Guide

How to calculate an IPv4 subnet step by step

Learn how to obtain network, broadcast and host range from an IPv4 and a CIDR prefix, with a /27 example solved by hand.

by Tools in a Tab · Published on · Updated

Short answer

To calculate an IPv4 subnet, separate the network bits from the host bits using the CIDR prefix. Set every host bit to zero to find the network address and to one to find the last address. For conventional /0–/30 subnets, reserve the network and broadcast addresses before counting usable hosts.

Let’s solve 10.20.30.200/27 by hand. Then you can check the result with the IPv4 subnet calculator.

Example result

Field Result
Input address 10.20.30.200/27
Mask 255.255.255.224
Network address 10.20.30.192/27
First regular host 10.20.30.193
Last regular host 10.20.30.222
Broadcast 10.20.30.223
Total addresses 32
Regular hosts 30

The address entered does not have to be the first in the block. In this case, .200 is an interior address of the subnet starting at .192.

Step 1: Interpret the /27 prefix

IPv4 has 32 bits. The number after the slash indicates how many bits make up the network part:

/27 = 27 network bits + 5 bits available inside the block

The mask places 27 ones in a row and fills in the remaining five bits with zeros:

11111111.11111111.11111111.11100000
255     .255     .255     .224

The first three octets contain 24 network bits. You just need to work with the last octet, where three network bits and five host bits remain.

Step 2: Calculate the block size

If five variable bits remain, there are 2⁵ combinations:

2^(32 - 27) = 2^5 = 32 addresses

You can also get the block size by subtracting the last mask octet from 256:

256 - 224 = 32

Therefore, the blocks of the last octet start at 0, 32, 64, 96, 128, 160, 192 and 224.

Step 3: Locate the address block

The last IP octet is 200. It lies between block starts 192 and 224, so it belongs to the block starting at 192. Equivalently, divide by the block size, round down, and multiply by the block size again:

floor(200 / 32) × 32 = 6 × 32 = 192

By retaining the first three octets, the network address is 10.20.30.192.

Step 4: Calculate the broadcast

The next block starts at 10.20.30.224. The address immediately before it is the end of the current block:

224 - 1 = 223

The broadcast is 10.20.30.223. In binary, the network address sets the five host bits to zero and the broadcast sets them all to one:

Network:   11000000 = 192
Broadcast: 11011111 = 223

You can obtain the same result by applying a bitwise AND to the IP and mask for the network address, then setting all host bits for the broadcast.

Step 5: Get the host range

In a conventional subnet, the network address and broadcast address are not assigned to hosts. The usual range is between the two:

First host: 10.20.30.192 + 1 = 10.20.30.193
Last host:  10.20.30.223 - 1 = 10.20.30.222

Of the 32 total addresses, 32 - 2 = 30 remain available for hosts in this conventional subnet model.

Quick method for /0 to /30

  1. Convert the prefix to a mask.
  2. Identify the octet where the mask stops being 255.
  3. Calculate the block size with 256 - the value of that octet.
  4. Round the corresponding IP octet down to a multiple of the block size.
  5. Keep earlier octets. For the network, use that multiple and set every later octet to zero. For the broadcast, use that multiple plus the block size minus one and set every later octet to 255.
  6. Add one to the network and subtract one from the broadcast to get the usual host range. Use the separate rules below for /31 and /32.

For 10.20.30.200/20, the mask is 255.255.240.0: the block size is 16 in the third octet. Rounding 30 down gives 16. The network is 10.20.16.0, the broadcast is 10.20.31.255, and the host range is 10.20.16.1–10.20.31.254. Keeping the original last octet .200 would be wrong. Check this second example with the same calculator.

Prefixes that should be recognized

Prefix Mask Addresses Regular hosts
/24 255.255.255.0 256 254
/25 255.255.255.128 128 126
/26 255.255.255.192 64 62
/27 255.255.255.224 32 30
/28 255.255.255.240 16 14
/29 255.255.255.248 8 6
/30 255.255.255.252 4 2

The usual 2^(32 - prefix) - 2 host formula does not apply to /31 and /32 in the models described next.

Special cases: /31 and /32

A /31 contains exactly two addresses. On a point-to-point link, both can identify the two ends; no broadcast address is reserved. This use is defined in RFC 3021.

A /32 contains one address and is used as a host route. There is no separate network or broadcast address to subtract from a host range.

Common errors when calculating subnets

  • Rounding to the nearest multiple instead of rounding down to the block start.
  • Confusing the input address with the network address.
  • Subtracting two addresses from a /31 or /32.
  • Accepting subnet masks with non-contiguous ones, such as 255.0.255.0.
  • Counting the prefix from the right; CIDR counts network bits from the left.
  • Confusing total addresses with usable hosts in a conventional subnet.

How to check your calculation

Enter 10.20.30.200 and /27 into the calculator. It should return network .192, broadcast .223 and range .193–.222. If you are reviewing an exercise, repeat the calculation in binary or with the block-size method before accepting it.

The tool works locally in the browser. It calculates mathematical block boundaries; it does not check whether an address is advertised, reserved, in use, or configured on a real network.

Technical references

RFC 4632 describes CIDR, the slash notation and IPv4 block sizes. RFC 3021 documents the treatment of /31 prefixes on point-to-point links.