Where the network part ends
An IPv4 address is 32 bits, written as four 8-bit octets. The first part of those bits names the network; the rest numbers a device on that network. The mask is the line between them. Here is 192.168.1.100/26:
/26 simply counts the 1 bits in the mask. Write those 26 ones as octets and you get 255.255.255.192: three full octets of eight 1s (255 each), then 11000000, which is 192.
The eight non-zero values a mask octet can take
Because the 1s come first, a mask octet can only be one of these values, or 0. Learn this row and you can convert any prefix:
| 1 bits in the octet | Binary | Value | Block size (256 − value) |
|---|---|---|---|
| 1 | 10000000 | 128 | 128 |
| 2 | 11000000 | 192 | 64 |
| 3 | 11100000 | 224 | 32 |
| 4 | 11110000 | 240 | 16 |
| 5 | 11111000 | 248 | 8 |
| 6 | 11111100 | 252 | 4 |
| 7 | 11111110 | 254 | 2 |
| 8 | 11111111 | 255 | 1 |
Memory aid: each value adds the next bit: 128, +64 = 192, +32 = 224, +16 = 240, +8 = 248, +4 = 252, +2 = 254, +1 = 255. The block size column is used in the step-by-step method.
Turning /20 into a dotted mask
- Divide the prefix by 8. The whole number is how many octets are 255.
- The remainder is how many 1 bits go in the next octet: look it up in the table.
- Any octets left over are 0.
| Prefix | Working | Mask |
|---|---|---|
| /20 | 20 = 2 × 8 + 4 → 255.255, then 4 bits = 240, then 0 | 255.255.240.0 |
| /27 | 27 = 3 × 8 + 3 → 255.255.255, then 3 bits = 224 | 255.255.255.224 |
| /12 | 12 = 1 × 8 + 4 → 255, then 240, then 0.0 | 255.240.0.0 |
Reading a dotted mask back as a prefix
Go the other way: count 8 for each 255, then add the number of 1 bits in the next octet. 255.255.255.248 is 8 + 8 + 8 + 5 = /29. 255.255.192.0 is 8 + 8 + 2 = /18.
You end up doing this both ways on the job. Cisco IOS takes the dotted mask on an interface (ip address 10.1.1.1 255.255.255.0), while show ip route and most documentation print the prefix, so the same subnet appears in both forms within a few lines of output.
A mask with a gap, such as 255.255.0.255 or 255.255.255.200, is not a valid subnet mask: the 1s must be continuous. (200 is 11001000, which has a 0 between 1s.)
How many hosts a prefix gives you
The host bits decide the size. With h host bits (32 − prefix):
- Addresses in the subnet = 2h.
- Usable hosts = 2h − 2. The first address is the network address and the last is the broadcast address; neither can be given to a device.
| Prefix | Host bits | Addresses | Usable hosts | Typical use |
|---|---|---|---|---|
| /20 | 12 | 4,096 | 4,094 | A campus building or a large site |
| /24 | 8 | 256 | 254 | A typical user VLAN |
| /26 | 6 | 64 | 62 | A department or guest Wi-Fi |
| /27 | 5 | 32 | 30 | Servers or printers |
| /30 | 2 | 4 | 2 | A router-to-router link |
The two exceptions. A /31 has no network or broadcast address: both of its two addresses are usable on a point-to-point link (RFC 3021). A /32 is a single address, used for loopback interfaces and host routes. The 2h − 2 rule applies from /30 downwards.
Test yourself on masks and prefixes
What is the dotted subnet mask for /20?
A router shows a mask of 255.255.255.248. What is the prefix length?
How many usable host addresses does a /27 subnet have?
Which of these is a valid subnet mask?
Make the conversions automatic
Masks become automatic with repetition. The subnetting practice drill asks unlimited questions and shows the worked answer; the CIDR calculator checks any conversion.
Learn more: A Step-by-Step MethodWildcard Masks