Beginner · IPv4 addressing · topic 13 of 57 · 30 min
Subnet masks, prefix length and wildcard masks
Explanation
Why a mask is needed
An address alone does not say where the network part ends. Is 192.168.10.77 on network 192.168.10.0, or 192.168.0.0, or 192.168.10.64? The subnet mask answers this. A device uses it to decide whether a destination is on its own network (send directly) or on another network (send to the default gateway, the router).
What a mask looks like
A subnet mask is also 32 bits. It is a run of ones followed by a run of zeros, never mixed. The ones cover the network part and the zeros cover the host part. 255.255.255.0 is 24 ones followed by 8 zeros: 11111111.11111111.11111111.00000000.
Because the ones are always together on the left, you can describe a mask by just counting them. That count is the prefix length, written after a slash: 255.255.255.0 is /24. This short form is called CIDR notation. 192.168.10.77/24 means "address 192.168.10.77 with a 24-bit mask".
The only valid mask octet values
Since the ones must be continuous from the left, a mask octet can only take nine values. Learn this table. It is the same as adding the place values from the left: 128, then 128 + 64 = 192, then 192 + 32 = 224, and so on.
| One bits in the octet | Binary | Decimal |
|---|---|---|
| 0 | 00000000 | 0 |
| 1 | 10000000 | 128 |
| 2 | 11000000 | 192 |
| 3 | 11100000 | 224 |
| 4 | 11110000 | 240 |
| 5 | 11111000 | 248 |
| 6 | 11111100 | 252 |
| 7 | 11111110 | 254 |
| 8 | 11111111 | 255 |
Converting prefix to mask and back
- /26: 26 = 8 + 8 + 8 + 2. Three full octets of 255, then 2 ones = 192. Mask 255.255.255.192.
- /20: 20 = 8 + 8 + 4. Two full octets, then 4 ones = 240, then 0. Mask 255.255.240.0.
- /12: 12 = 8 + 4. One full octet, then 240, then 0, 0. Mask 255.240.0.0.
- 255.255.248.0 back to a prefix: 8 + 8 + 5 (248 has five ones) + 0 = /21.
- 255.255.255.252: 8 + 8 + 8 + 6 = /30, the classic mask for a link between two routers.
The AND operation
To find the network, a device does a bitwise AND of the address and the mask. AND compares two bits: the result is 1 only if both bits are 1. So where the mask has ones, the address bit is kept; where the mask has zeros, the bit becomes 0. The host part is wiped out and only the network part is left.
| Binary | Decimal | |
|---|---|---|
| Address | 11000000.10101000.00001010.01001101 | 192.168.10.77 |
| Mask /27 | 11111111.11111111.11111111.11100000 | 255.255.255.224 |
| AND result | 11000000.10101000.00001010.01000000 | 192.168.10.64 |
With a /24 mask the same address gives 192.168.10.0. Same address, different mask, different network. That is why the mask matters as much as the address.
Wildcard masks
Cisco ACLs (access control lists) and the OSPF network command use a wildcard mask instead of a subnet mask. A wildcard mask is the inverse: a 0 bit means "this bit must match" and a 1 bit means "ignore this bit". To get the wildcard for a normal subnet, subtract each mask octet from 255.
| Prefix | Subnet mask | Wildcard mask |
|---|---|---|
| /24 | 255.255.255.0 | 0.0.0.255 |
| /26 | 255.255.255.192 | 0.0.0.63 |
| /30 | 255.255.255.252 | 0.0.0.3 |
| /20 | 255.255.240.0 | 0.0.15.255 |
| /16 | 255.255.0.0 | 0.0.255.255 |
R1(config)# interface GigabitEthernet0/0
R1(config-if)# ip address 192.168.10.1 255.255.255.0
R1(config-if)# exit
R1(config)# access-list 10 permit 192.168.10.0 0.0.0.255
R1(config)# router ospf 1
R1(config-router)# network 10.1.0.0 0.0.255.255 area 0import ipaddress
net = ipaddress.ip_network("192.168.10.64/27")
print(net.netmask) # 255.255.255.224
print(net.hostmask) # 0.0.0.31 (the wildcard mask)
print(net.prefixlen) # 27Common mistakes
- Invalid octets: 255.255.255.100 or 255.255.250.0 cannot be masks, because 100 and 250 are not in the table of nine values.
- Non-continuous masks like 255.0.255.0 are not valid subnet masks.
- Counting /24 as 24 hosts. The prefix counts network bits; the host bits are 32 minus the prefix.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Subnet masks, prefix length and wildcard masks · 1/7
An address alone does not tell you where the network part ends. The subnet mask does.
Prefer a video by a teacher? Find free videos on “subnet mask prefix length wildcard mask explained ccna” on YouTube ↗
In real life: A wrong ACL at a bank branch
A network engineer at a bank branch in Jaipur must allow only the teller subnet 10.8.20.0/24 to reach the core banking server. A junior colleague wrote access-list 20 permit 10.8.20.0 255.255.255.0, thinking of the subnet mask. IOS read it as a wildcard: ignore the first three octets, and the last octet must be 0. The rule matched only addresses ending in .0, so no teller could reach the server on Monday morning. Rewriting it with the wildcard 0.0.0.255 fixed it in one line.
! wrong: matches any address ending in .0
access-list 20 permit 10.8.20.0 255.255.255.0
! right: only 10.8.20.0 to 10.8.20.255
access-list 20 permit 10.8.20.0 0.0.0.255Practice (0/4 done)
Write prefix_to_mask(prefix) that turns a prefix length (0 to 32) into a dotted decimal mask. Example: prefix_to_mask(26) returns "255.255.255.192". Hint: build a string of prefix ones followed by zeros, 32 characters long, then cut it into four 8-bit pieces.
Write mask_to_prefix(mask) that counts the one bits in a dotted decimal mask. Example: mask_to_prefix("255.255.240.0") returns 20. You may use bin(number).count("1").
Write wildcard(mask) that returns the wildcard mask for a subnet mask by subtracting each octet from 255. Example: wildcard("255.255.255.192") returns "0.0.0.63".
Which of these is NOT a valid subnet mask?
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Network, broadcast and usable host rangeIPv4 addressing · same module
- Subnetting a network into equal subnetsSubnetting practice and IPv6
- Access control lists (ACLs)Network security
- Configuring and verifying single-area OSPFRouting: static routes and OSPF
- IP addresses, subnets and CIDRCyber Security: from zero to SOC analyst · Networking for security
- VLSM: subnets of different sizesSubnetting practice and IPv6
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is the dotted decimal mask for /27?
Answer: 255.255.255.224
/27 = 24 + 3: three ones in the last octet = 128 + 64 + 32 = 224.
What prefix length is 255.255.252.0?
Answer: /22
8 + 8 + 6 (252 has six ones) + 0 = 22.
What is the wildcard mask for 255.255.255.240?
Answer: 0.0.0.15
255 minus 240 is 15, so the wildcard is 0.0.0.15.
What is 172.16.45.200 AND 255.255.240.0?
Answer: 172.16.32.0
In the third octet, 45 AND 240: 00101101 AND 11110000 = 00100000 = 32. The last octet becomes 0.
Where does Cisco IOS expect a wildcard mask instead of a subnet mask?
Answer: In access-list statements and the OSPF network command
Interfaces take subnet masks. ACLs and OSPF network statements take wildcard masks.
What is the network address of 192.168.5.130/25?Related
Answer: 192.168.5.128
/25 has a block size of 128. 130 is in 128 to 255, so the network is 192.168.5.128.
Each subnet must hold 25 hosts. Which prefix wastes the fewest addresses?Related
Answer: /27
/27 gives 30 usable hosts. /28 gives only 14, and /26 wastes more.
What is the wildcard mask for the subnet 172.16.8.0/22?Related
Answer: 0.0.3.255
A /22 mask is 255.255.252.0; 255 minus each octet gives 0.0.3.255.
Are 10.1.1.100/25 and 10.1.1.130/25 in the same subnet?Related
Answer: No
/25 splits the octet at 128. 100 is in 0 to 127, 130 is in 128 to 255, so they are in different subnets.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “IPv4 addressing” before its exam: Network, broadcast and usable host range, Validating addresses and checking the same subnet.
- Module 3 exam: IPv4 addressing · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
- Then the next module, Subnetting practice and IPv6. 57 topics left in the course before the final exam.