A subnet mask is a 32-bit pattern of ones followed by zeros that marks which bits of an address name the network and which name the host. ANDing an address with its mask gives the network address, setting every host bit to one gives the broadcast address, and a subnet with h host bits has 2^h - 2 usable addresses. For 172.16.37.19 with mask 255.255.240.0 (prefix /20) the network is 172.16.32.0, the broadcast is 172.16.47.255, and there are 4,094 usable hosts.

Part of Networking

Dividing an address block into subnets with a mask: the binary method, a table of common sizes, and two fully worked examples.

The Mask Splits Every Address Into A Network Part And A Host Part

Network Addressing introduces the mask with one example, 192.168.1.42 with 255.255.255.0, giving network 192.168.1.0, broadcast 192.168.1.255 and 254 hosts. Routing Basics explains why it matters: a host compares a destination with its own address under its mask, and sends directly if they are on the same subnet or to the default gateway if not. This article does the arithmetic for masks that do not fall on a byte boundary, which is where mistakes happen. Read IPv4 Structure first for converting octets to binary.

A valid mask is a run of ones followed by a run of zeros. The number of ones is the prefix length, written after a slash (CIDR Notation). A /24 has 24 ones and 8 host bits; a /20 has 20 ones and 12 host bits.

What You Need

  • The address and its mask or prefix length
  • Binary for the interesting octet only — the octets the mask fills with 255 or 0 need no conversion

Method: From Address And Mask To The Whole Subnet

Step 1: Count host bits Host bits h = 32 - prefix length.

Step 2: Find the interesting octet The octet where the mask is neither 255 nor 0. Write the address and mask octets in binary.

Step 3: AND them bit by bit A result bit is 1 only where both bits are 1. The octets before it keep the address value, the octets after it become 0. That is the network address.

Step 4: Set all host bits to 1 for the broadcast OR the interesting octet with the inverse of the mask, and make the later octets 255.

Step 5: Usable hosts Total addresses 2^h, minus the network address and the broadcast address: 2^h - 2. The first usable is network + 1 and the last is broadcast - 1.

Each Extra Network Bit Halves The Number Of Hosts

PrefixDotted maskHost bitsTotal addressesUsable hosts
/24255.255.255.08256254
/25255.255.255.1287128126
/26255.255.255.19266462
/27255.255.255.22453230
/28255.255.255.24041614
/29255.255.255.248386
/30255.255.255.252242

The /24, /28 and /30 rows agree with the figures in Routing Basics: 254 hosts, 14 hosts, and 2 hosts for a link between two routers. The dotted mask comes from the last octet of the prefix: for /26 the last octet has 2 ones, 11000000 = 128 + 64 = 192.

Worked Example 1: Four Subnets From One /24

Block 192.168.1.0/24, to be split into four equal subnets. Four subnets need 2 extra network bits (2^2 = 4), so the prefix becomes 24 + 2 = 26, leaving 32 - 26 = 6 host bits. The last octet of the mask is 11000000 = 192, so the mask is 255.255.255.192. Each subnet holds 2^6 = 64 addresses, 64 - 2 = 62 usable, and the subnets start 64 apart.

SubnetNetworkFirst usableLast usableBroadcast
1192.168.1.0/26192.168.1.1192.168.1.62192.168.1.63
2192.168.1.64/26192.168.1.65192.168.1.126192.168.1.127
3192.168.1.128/26192.168.1.129192.168.1.190192.168.1.191
4192.168.1.192/26192.168.1.193192.168.1.254192.168.1.255

Which subnet does 192.168.1.100 belong to? Last octet 100 = 01100100; mask 192 = 11000000; AND = 01000000 = 64. It is in 192.168.1.64/26. Its broadcast: set the 6 host bits to 1, 01000000 OR 00111111 = 01111111 = 127, giving 192.168.1.127. The check: 64 + 62 + 1 = 127.

Worked Example 2: A Mask That Crosses A Byte Boundary

Address 172.16.37.19, mask 255.255.240.0, which is prefix /20 (8 + 8 + 4 ones). Host bits = 32 - 20 = 12.

The interesting octet is the third. 37 = 00100101; 240 = 11110000. AND: 00100000 = 32. So the network is 172.16.32.0, with the fourth octet 0 because its mask is 0.

Broadcast: third octet 00100000 OR 00001111 = 00101111 = 47; fourth octet 255. Broadcast 172.16.47.255.

Usable hosts: 2^12 = 4,096 total; 4,096 - 2 = 4,094. Range 172.16.32.1 to 172.16.47.254. Check: the subnet spans third octet values 32 to 47, which is 16 values; 16 x 256 = 4,096. ✓

Two Machines Decide Who Is Local By The Mask, So Both Must Agree

Host A is 10.1.0.5/24 and host B is 10.1.1.9/24. AND each with 255.255.255.0: A is on 10.1.0.0, B on 10.1.1.0. They differ, so A sends to its default gateway. If both had mask 255.255.254.0 (/23), A would be on 10.1.0.0 and B also on 10.1.0.0, because the third octet 1 = 00000001 ANDed with 254 = 11111110 gives 0, so they would talk directly. Two machines with different masks can disagree about whether they are neighbours, which is why one subnet needs one mask everywhere.

Common Mistakes

  • Forgetting the minus 2 — the network address and broadcast address cannot be given to machines, so a /24 holds 254, not 256
  • Starting a subnet at an address that is not on the boundary — 192.168.1.50/26 is a host address inside 192.168.1.0/26, not a network
  • Using a mask whose ones are not contiguous — 255.0.255.0 is not a valid mask
  • Different masks on the two ends of a link — the machines disagree about which addresses are local

What’s Next

  • Learn next: CIDR Notation — slash notation, fitting several subnets of different sizes, and combining routes
  • Combine with: Routing Tables — where the mask is used on every forwarded packet