ZNetLab › Published › Article

Subnetting without the magic-number trick

ArticleFundamentals@admin2 min read

Subnetting without the magic-number trick

The mask is a ruler, not a riddle. Four questions answer every subnetting problem you will be asked, and none of them needs a table.

Most people meet subnetting as a trick: subtract from 256, count by the answer, done. It works, and it is why so many people can subnet a /26 in their sleep and stall completely on a /19. The trick is a shortcut through one idea, and the idea is simpler than the shortcut.

An address is two fields, and the mask says where the join is

An IPv4 address is 32 bits. The mask is 32 bits as well, and every bit of it answers one question about the matching address bit: is this bit part of the network, or part of the host? A 1 means network. A 0 means host. That is the whole definition. 255.255.255.0 is twenty-four 1s followed by eight 0s, which is why it is also written /24.

Everything else follows from that one sentence.

Four questions

Given 172.16.37.100/20, the four things anybody ever asks are:

/20 means twenty network bits, so twelve host bits. Twelve zeroes in the last twelve positions: the third octet keeps its top four bits and loses the bottom four. 37 is 00100101; its top four bits are 0010, which is 32. So the network is 172.16.32.0, the broadcast is 172.16.47.255, the usable range is 172.16.32.1 to 172.16.47.254, and there are 2^12 − 2 = 4094 hosts.

Why minus two

The all-zeroes host is the network itself and the all-ones host is the directed broadcast. Neither can be given to an interface. On a point-to-point link that leaves /30 giving you two usable addresses out of four, which is the reason /31 exists and is worth knowing about: RFC 3021 says a /31 on a point-to-point link has no network or broadcast address, because there is nobody to broadcast to. Two addresses, both usable.

Check it on something that answers back

The arithmetic is worth doing on paper once and then never trusting again. Put two PCs either side of a router in the simulator, give one a /24 and the other a /20 that overlaps it, and watch what the router does: it will route between them, and the host with the wrong mask will ARP for a destination it believes is local and get nothing. That failure — ping works one way and not the other — is a mask problem nine times out of ten, and it looks nothing like a mask problem until you have seen it once.

subnettingccnaipv4fundamentals

Join the discussion

Replies, likes and bookmarks live in the community half, which needs a free account. Writing here is free too, and everything is reviewed before it is published.

Open this in the communityEverything publishedHow this works