Skip to content

Beginner · IPv4 addressing · topic 13 of 57 · 30 min

Subnet masks, prefix length and wildcard masks

Explanation

A subnet mask is 32 bits: ones over the network part and zeros over the host part. You can write it in dotted decimal (255.255.255.0) or as a prefix length (/24), the number of one bits. A device finds its network by doing a bitwise AND of its address and mask. A wildcard mask is the opposite of a subnet mask and is used in Cisco ACLs and OSPF.

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 octetBinaryDecimal
0000000000
110000000128
211000000192
311100000224
411110000240
511111000248
611111100252
711111110254
811111111255

Converting prefix to mask and back

  1. /26: 26 = 8 + 8 + 8 + 2. Three full octets of 255, then 2 ones = 192. Mask 255.255.255.192.
  2. /20: 20 = 8 + 8 + 4. Two full octets, then 4 ones = 240, then 0. Mask 255.255.240.0.
  3. /12: 12 = 8 + 4. One full octet, then 240, then 0, 0. Mask 255.240.0.0.
  4. 255.255.248.0 back to a prefix: 8 + 8 + 5 (248 has five ones) + 0 = /21.
  5. 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.

BinaryDecimal
Address11000000.10101000.00001010.01001101192.168.10.77
Mask /2711111111.11111111.11111111.11100000255.255.255.224
AND result11000000.10101000.00001010.01000000192.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.

PrefixSubnet maskWildcard mask
/24255.255.255.00.0.0.255
/26255.255.255.1920.0.0.63
/30255.255.255.2520.0.0.3
/20255.255.240.00.0.15.255
/16255.255.0.00.0.255.255
output
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 0
Cisco IOS: an interface takes a subnet mask, while an ACL and the OSPF network command take wildcard masks.
python
import 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)   # 27
Python: hostmask is the same thing as the wildcard mask.

Common mistakes

Careful: Writing a subnet mask where IOS wants a wildcard. access-list 10 permit 192.168.10.0 255.255.255.0 does not mean "the 192.168.10.0/24 network". IOS reads the three 255s as "ignore the first three octets" and the 0 as "the last octet must match 0", so the rule matches any address ending in .0 and none of the hosts you meant. Always write the wildcard, 0.0.0.255.
  • 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

Subnet masks

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.

output
! 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.255
Cisco IOS: lines starting with ! are comments.

Practice (0/4 done)

Task 1 · Code · Python

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.

Task 2 · Code · Python

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").

Task 3 · Code · Python

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".

Task 4 · Question

Which of these is NOT a valid subnet mask?

Quick quiz

Question 1

1.What is the dotted decimal mask for /27?

Question 2

2.What prefix length is 255.255.252.0?

Question 3

3.What is the wildcard mask for 255.255.255.240?

Question 4

4.What is 172.16.45.200 AND 255.255.240.0?

Question 5

5.Where does Cisco IOS expect a wildcard mask instead of a subnet mask?

Read more (free)

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.

From Network, broadcast and usable host range

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.

From Subnetting a network into equal subnets

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.

From Access control lists (ACLs)

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.

From Validating addresses and checking the same subnet

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

  1. 2 more topics in “IPv4 addressing” before its exam: Network, broadcast and usable host range, Validating addresses and checking the same subnet.
  2. Module 3 exam: IPv4 addressing · 13 questions · 20 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Subnetting practice and IPv6. 57 topics left in the course before the final exam.

← Previous: IPv4 address structure and special ranges