Course contents

Chapter 2: IP Subnetting and VLSMLesson 2 of 8

Subnet masks and CIDR

The mask says where the network part of an address ends. Learn to read it in binary and convert it in your head.

Intermediate · 7 min read · Before this: Subnetting

Technical definition

Subnet mask is a 32-bit value that marks which bits of an IPv4 address are the network part (1 bits) and which are the host part (0 bits). All the 1 bits come first. CIDR notation writes the same mask as a prefix length: the number of 1 bits, for example /26.

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:

Each octet: address bits above, mask bits below.Network: 26 bitsHost: 6 bits
/26: 26 network bits (mask 1s), then 6 host bits (mask 0s). The line falls inside the 4th octet.

/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 octetBinaryValueBlock size (256 − value)
110000000128128
21100000019264
31110000022432
41111000024016
5111110002488
6111111002524
7111111102542
8111111112551

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

  1. Divide the prefix by 8. The whole number is how many octets are 255.
  2. The remainder is how many 1 bits go in the next octet: look it up in the table.
  3. Any octets left over are 0.
Diagram
Turning /20 into a dotted mask
Turning /20 into a dotted mask
PrefixWorkingMask
/2020 = 2 × 8 + 4 → 255.255, then 4 bits = 240, then 0255.255.240.0
/2727 = 3 × 8 + 3 → 255.255.255, then 3 bits = 224255.255.255.224
/1212 = 1 × 8 + 4 → 255, then 240, then 0.0255.240.0.0
Read each row across: the left box names the item, the others describe it.
Each octet: address bits above, mask bits below.Network: 20 bitsHost: 12 bits
/20: the line falls inside the 3rd octet, so the mask there is 240 and the whole 4th octet is host bits.

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.
PrefixHost bitsAddressesUsable hostsTypical use
/20124,0964,094A campus building or a large site
/248256254A typical user VLAN
/2666462A department or guest Wi-Fi
/2753230Servers or printers
/30242A 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

Predict · scenario 1

What is the dotted subnet mask for /20?

Predict · scenario 2

A router shows a mask of 255.255.255.248. What is the prefix length?

Predict · scenario 3

How many usable host addresses does a /27 subnet have?

Predict · scenario 4

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