How to calculate a subnet by hand
An IPv4 address is 32 bits. The prefix (the number after the slash) says how many of those bits, counting from the left, identify the network; the rest identify the host within that network. Everything follows from that:
- Network address: keep the network bits and set the host bits to 0. It is exactly a bitwise AND between the IP and the subnet mask.
- Broadcast address: the same network bits with every host bit set to 1.
- Host range: from network + 1 to broadcast − 1.
- Number of usable hosts: 2host bits − 2, because the network and broadcast addresses cannot be assigned to devices.
The shortcut that saves the most time in exams is the interesting octet: find the octet where the mask is neither 255 nor 0 and compute the block size as 256 minus that value. Subnets start at multiples of that block size.
Example 1 · 192.168.1.130/26
/26 leaves 6 host bits in the last octet, so the mask is 255.255.255.192 and the block size is 256 − 192 = 64. Subnets in the last octet start at 0, 64, 128 and 192. 130 falls between 128 and 191, so:
Network 192.168.1.128 · broadcast 192.168.1.191 · hosts .129 to .190 · 26 − 2 = 62 hosts.
Example 2 · 172.16.45.200/20
/20 is the 16 bits of the first two octets plus 4 bits of the third: mask 255.255.240.0. The interesting octet is the third one and the block size is 256 − 240 = 16, with subnets at 0, 16, 32, 48… 45 lies between 32 and 47:
Network 172.16.32.0 · broadcast 172.16.47.255 · hosts 172.16.32.1 to 172.16.47.254 · 212 − 2 = 4,094 hosts.
Subnet mask cheat sheet
| Prefix | Subnet mask | Wildcard | Addresses | Usable hosts |
|---|---|---|---|---|
| /8 | 255.0.0.0 | 0.255.255.255 | 16,777,216 | 16,777,214 |
| /16 | 255.255.0.0 | 0.0.255.255 | 65,536 | 65,534 |
| /20 | 255.255.240.0 | 0.0.15.255 | 4,096 | 4,094 |
| /22 | 255.255.252.0 | 0.0.3.255 | 1,024 | 1,022 |
| /24 | 255.255.255.0 | 0.0.0.255 | 256 | 254 |
| /25 | 255.255.255.128 | 0.0.0.127 | 128 | 126 |
| /26 | 255.255.255.192 | 0.0.0.63 | 64 | 62 |
| /27 | 255.255.255.224 | 0.0.0.31 | 32 | 30 |
| /28 | 255.255.255.240 | 0.0.0.15 | 16 | 14 |
| /29 | 255.255.255.248 | 0.0.0.7 | 8 | 6 |
| /30 | 255.255.255.252 | 0.0.0.3 | 4 | 2 |
What is VLSM and how do you plan it?
VLSM (Variable Length Subnet Masking) means splitting a block into subnets of different sizes, each one fitted to the hosts it needs, instead of cutting it into equal pieces. The method is always the same:
- Sort the subnets from most to fewest hosts.
- For each one, find the smallest h such that 2h − 2 is at least the number of hosts. Its prefix is 32 − h.
- Assign them in order, each one starting right after the previous subnet's broadcast address.
Example 3 · 192.168.10.0/24 for 100, 50, 20 and 2 hosts
100 hosts need 27 − 2 = 126 → /25: 192.168.10.0/25 (.1–.126). 50 hosts need 26 − 2 = 62 → /26: 192.168.10.128/26 (.129–.190). 20 hosts need 25 − 2 = 30 → /27: 192.168.10.192/27 (.193–.222). The 2-host link is a /30: 192.168.10.224/30 (.225–.226). Addresses from .228 onwards remain free.
Frequently asked questions
Why do you subtract 2 from the number of hosts?
The first address of each subnet (all host bits 0) identifies the network and the last one (all host bits 1) is the broadcast. Neither can be assigned to a device, except in the special /31 and /32 cases.
What about /31 and /32?
A /31 has only two addresses and, according to RFC 3021, both are used as hosts on point-to-point links between routers. A /32 identifies a single device and is used for host routes or loopback interfaces.
What is a wildcard mask used for?
It is the inverted subnet mask (255 minus each octet of the mask). Cisco ACLs and OSPF use it to specify which bits must match: a 0 in the wildcard means "this bit must be equal" and a 1 means "don't care".
Do IP address classes A, B and C still exist?
Not in modern routing, which has used CIDR since 1993. They are shown because many courses (including CCNA material) still mention them and because the default masks (/8, /16 and /24) come from them.
More computer science tools
Two's Complement Calculator
Signed integers in two's complement, one's complement and sign-magnitude, plus overflow.
OpenIEEE 754 Floating-Point Converter
32 and 64-bit floating point with biased exponent and rounding error.
OpenKarnaugh Map Solver
Minimal SOP and POS expressions with the groups highlighted on the map.
OpenTruth Table Generator
Truth table for any expression, with minterms and canonical forms.
OpenNumber Base Converter
Binary, octal, decimal, hex and bases 2–36 with repeated division.
OpenSorting Algorithm Visualizer
Animated sorting with comparison and write counters.
Open