DRV-019
Subnetting, and the two addresses you cannot use
The setup
An IPv4 address is 32 bits. A prefix length p fixes the leading p bits as the network and leaves 32 − p host bits free, so a subnet contains 2^(32−p) addresses. Two of them are not assignable to a host.
The derivation
Step 1: Count the addresses
Each free host bit doubles the space.
addresses = 2^(32 - p)Step 2: Remove the two reserved addresses
All host bits zero is the network address, which names the subnet itself. All host bits one is the broadcast address. Neither can identify an individual host.
usable = 2^(32 - p) - 2Step 3: Find the boundaries by bit arithmetic
The network address is the IP ANDed with the mask; the broadcast is the IP ORed with the inverted mask.
network = IP AND mask broadcast = IP OR (NOT mask)usable_hosts = 2^(32 - prefix) - 2Checks that prove it is right
A formula you cannot test is a formula you have to trust. These take seconds, and they are what separate a derivation from a formula restatement.
p = 24 → 256 addresses, 254 usable
The most familiar case, and the one every network engineer can check by eye.
p = 31 → 2 addresses, both usable
RFC 3021 defines /31 for point-to-point links, where there is no broadcast to reserve. The minus-two rule would give zero, which is why a calculator that applies it blindly is wrong here.
p = 32 → A single host route
Not a subnet at all. The minus-two rule would give minus one.
Worked example
192.168.1.130/26.
The mask is 255.255.255.192. ANDing gives a network of 192.168.1.128 and ORing the inverted mask gives a broadcast of 192.168.1.191. The subnet holds 64 addresses of which 62 are usable, running from .129 to .190. Note that .130 sits inside the second /26 of that /24, not the first, which is the kind of thing that is obvious from the bit arithmetic and easy to get wrong by counting.
Computed by the network-addressing engine and verified by hand against the mask.
What it assumes
- Classless addressing throughout; classful rules have not applied since 1993.
Where the formula stops being valid
Stating limits plainly is more useful than pretending there are none, and it is the item competitors most consistently omit.
- IPv6 has no broadcast address, so no subtraction applies at all. A /64 has 2^64 usable addresses, not 2^64 − 2.
- Some equipment reserves an additional address for a gateway by convention, which is a local policy rather than a property of the protocol.