Subnetting Math: Demystifying CIDR & Host Ranges
How to calculate network addresses, broadcast addresses, and usable host counts from any IPv4 CIDR prefix without guesswork.
What is CIDR?
Classless Inter-Domain Routing (CIDR) replaced the old Class A, B, and C system with a flexible prefix length notation (`/XX`).
An IPv4 address consists of 32 bits, represented as 4 octets separated by dots (e.g., `192.168.1.0`). The prefix length tells you how many of those 32 bits belong to the **Network ID**, while the remaining bits belong to **Host IDs**.
Key Formulas
1. **Total IP Addresses in a Subnet**: $$\text{Total IPs} = 2^{(32 - \text{prefix})}$$
2. **Usable Host Addresses**: $$\text{Usable Hosts} = 2^{(32 - \text{prefix})} - 2$$ *(Subtract 2 because the First address is the Network ID and the Last address is the Broadcast ID)*.
Cheat Sheet: Common Prefix Sizes
| Prefix | Subnet Mask | Total IPs | Usable Hosts | Common Use Case |
| `/30` | `255.255.255.252` | 4 | 2 | Point-to-Point Router Links |
| `/28` | `255.255.255.240` | 16 | 14 | Small Subnet / Cloud DMZ |
| `/24` | `255.255.255.0` | 256 | 254 | Standard LAN / Home Network |
| `/20` | `255.255.240.0` | 4,096 | 4,094 | Corporate Branch Network |
| `/16` | `255.255.0.0` | 65,536 | 65,534 | AWS VPC Primary CIDR Block |
Step-by-Step Example: `172.16.50.0/26`
- **Prefix length**: `26` $\rightarrow$ Host bits = $32 - 26 = 6$ bits. - **Total addresses**: $2^6 = 64$ IPs. - **Usable hosts**: $64 - 2 = 62$ hosts. - **Subnet Mask**: `255.255.255.192` - **Network Address**: `172.16.50.0` - **First Usable Host**: `172.16.50.1` - **Last Usable Host**: `172.16.50.62` - **Broadcast Address**: `172.16.50.63`