IPv4 Subnetting & CIDR: Masks, Broadcast and Usable Host Math

Start with "Why 254, Not 256"

A /24 is usually described as "254 usable hosts", not 256, and the reason is binary. IPv4 is 32 bits; /24 fixes the first 24 as the network number, leaving 8 host bits. Eight bits give 256 combinations (0–255), but two must be reserved:

all-zero host bits  →  network address (identifies the subnet)
all-one host bits   →  broadcast address (sent to every host)

Remove those two and 256 − 2 = 254 usable hosts. The rule 2^(32−prefix) − 2 holds at any prefix and underpins every calculation below.

Two Notations: Mask vs Prefix

CIDR Mask Network bits Usable hosts
/30 255.255.255.252 30 2
/29 255.255.255.248 29 6
/28 255.255.255.240 28 14
/24 255.255.255.0 24 254
/23 255.255.254.0 23 510
/16 255.255.0.0 16 65,534
/8 255.0.0.0 8 16,777,214

Convert the mask to binary and count the ones to get the prefix. See /16 and you know there are 16 host bits with 65,534 usable; see /30 and you know it is a point-to-point link.

Three Calculations You Must Master

1. Network Address (AND)

IP        11000000.10101000.00000101.00100101   (192.168.5.37)
mask /26  11111111.11111111.11111111.11000000   (255.255.255.192)
──────────────────────────────────────────────
AND       11000000.10101000.00000101.00000000   → 192.168.5.0

Clearing the host bits yields the network address.

2. Broadcast Address (Network OR All-One Host Bits)

In the same subnet, set every host bit to 1:

192.168.5.0 + (63)   →  192.168.5.63

At a non-byte boundary, the key is "stride = 256 − last decimal octet of the mask". A mask of 255.255.255.192 gives stride 64, so the subnet spans 192.168.5.0–.63 with usable hosts .1–.62.

3. Usable Host Count

2^(32−prefix) − 2: /24 → 254; /26 → 62; /30 → 2.

Network = IP AND mask; broadcast = network OR all-one host bits; usable = 2^(host bits) − 2.

Cloud Architecture Advice

  • Start a VPC with /16: leave room for multiple environments (dev/staging/prod) and subnets;
  • /24 per environment: 254 hosts is plenty to start, easy to remember and manage;
  • /30 for point-to-point (VPN/links): wastes only 2 addresses and barely occupies a range;
  • Watch reserved addresses: cloud vendors usually reserve the first four and the last of each subnet (gateway, DNS, NAT, spare); account for them per their docs;
  • Avoid /28 or smaller: they exhaust quickly and are hard to expand.

Reproducible Results

The mask, total addresses, usable hosts and broadcast of a /26, in one stdlib call, matching the IPv4 subnet calculator:

import ipaddress
net = ipaddress.ip_network('192.168.1.0/26')
print(net.netmask)              # → 255.255.255.192
print(net.num_addresses)        # → 64 (total addresses)
print(net.num_addresses - 2)    # → 62 (usable: minus network and broadcast)
print(net.broadcast_address)    # → 192.168.1.63
print(net[1], net[-2])          # → 192.168.1.1 192.168.1.62 (first/last usable)

Each +2 of prefix halves the host bits: /25=128, /26=64, /27=32. Remember usable = 2^(32−prefix) − 2 and you can drop the calculator.

Frequently Asked Questions

What does /24 actually mean and how many hosts fit?

/24 means the mask has 24 leading 1-bits (255.255.255.0): the first three octets are network bits and the last 8 are host bits. 8 host bits give 2^8=256 addresses, but the network address (all-zero) and broadcast (all-one) are reserved, so 254 usable hosts. Likewise /25=128-2=126 hosts, /26=64-2=62, /30=4-2=2 (a classic point-to-point link). The formula is 2^(32-prefix) - 2.

How do subnet masks and CIDR prefixes relate?

They are two notations for the same thing. The dotted-decimal mask (255.255.255.0) and the slash prefix (/24) both state how many leading bits are network bits. Convert the mask to binary and count the 1s to get the prefix: 255.255.255.0 → 24 ones → /24. See /16 (255.255.0.0) and you know there are 16 host bits (65,534 usable); see /30 and you know it is a point-to-point link with 2 usable addresses.

How do I quickly compute a network and broadcast address?

Use bitwise logic: network = IP AND mask (host bits cleared); broadcast = network OR (inverted mask, host bits all 1). Example: 192.168.5.37/24 → mask 255.255.255.0 → network 192.168.5.0, broadcast 192.168.5.255. For a non-octet boundary like 192.168.5.37/26 (mask 255.255.255.192), network is 192.168.5.0 and broadcast 192.168.5.63—remember the subnet increments by 64 and you can do it in your head.

Why do VPC and cloud environments recommend starting with /16 or /24?

In the cloud, prefer coarse subnetting: start a VPC with /16 (65,534 usable) so you can slice /24s per environment (dev/staging/prod). Cores start at /24 (254 usable). A /28 or /29 exhausts IPs quickly and is hard to expand; a /8 grows a huge broadcast domain and route table. Common practice: VPC /16, environment /24, point-to-point /30, while avoiding reserved addresses (clouds reserve the first few and last few of each subnet).

← Back to Blog