Sub topic 6
Subnet Masks and the Bitwise AND
This chapter assumes you can convert values from 0 to 255 between binary and decimal. It also assumes you know that an IPv4 address splits into a network part and a host part, and that CIDR writes the split as a prefix length such as /20. Here you will see how a device uses that prefix length: it turns the length into a subnet mask, then combines the mask with an address using a bitwise AND.
What a subnet mask is
A subnet mask is a 32-bit value written in dotted decimal, like an IPv4 address. Its bits are:
- 1 in every position that belongs to the network part.
- 0 in every position that belongs to the host part.
A valid mask is always a run of 1s followed by a run of 0s. The prefix length is the number of 1s, so /20 means twenty 1s followed by twelve 0s:
11111111.11111111.11110000.00000000 = 255.255.240.0The prefix and the mask carry the same information. The prefix is compact and easy to read. The mask is what hardware and operating systems use internally, and what older tools and configuration screens still ask for.
Converting between prefix and mask
Fill the prefix's 1s into the octets from the left. Each full octet of 1s is 255. One partial octet has a value from the table below. Any octet after it is 0.
| Bits set in the octet | Binary | Decimal |
|---|---|---|
| 1 | 10000000 | 128 |
| 2 | 11000000 | 192 |
| 3 | 11100000 | 224 |
| 4 | 11110000 | 240 |
| 5 | 11111000 | 248 |
| 6 | 11111100 | 252 |
| 7 | 11111110 | 254 |
| 8 | 11111111 | 255 |
For /20, the first two octets are full (16 bits). The remaining 4 bits fall in the third octet, which gives 240. The mask is 255.255.240.0.
For /27, three octets are full (24 bits). The remaining 3 bits give 224 in the fourth octet. The mask is 255.255.255.224.
To go the other way, count the 1s. 255.255.248.0 has 8 + 8 + 5 = 21 ones, so it is /21.
The bitwise AND
A bitwise AND compares two numbers bit by bit. The result bit is 1 only when both input bits are 1.
| Address bit | Mask bit | Result |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 1 |
This table has two consequences:
- Where the mask bit is 1, the result copies the address bit. The network part passes through unchanged.
- Where the mask bit is 0, the result is 0 whatever the address says. The host part is erased.
So address AND mask gives the network address, the lowest address in the subnet, which has all host bits set to 0.
Worked example: 172.16.37.200/20
The mask is 255.255.240.0. Octets that are 255 or 0 in the mask need no calculation. A 255 keeps the octet and a 0 clears it. Only the third octet needs a real AND:
address third octet: 37 = 00100101
mask third octet: 240 = 11110000
AND 00100000 = 32The first two octets pass through (172.16), and the fourth is cleared (0). The network address is 172.16.32.0.
Worked example: 192.168.10.77/26
The mask is 255.255.255.192. Only the fourth octet needs work:
address fourth octet: 77 = 01001101
mask fourth octet: 192 = 11000000
AND 01000000 = 64The network address is 192.168.10.64.
Shortcut with block size
Subtracting the mask's interesting octet from 256 gives the block size. For 255.255.255.192, 256 − 192 = 64. Subnet boundaries in that octet are multiples of 64: 0, 64, 128, 192. The network address is the largest multiple that does not exceed the host value. Since 77 lies between 64 and 128, the network is .64. This matches the AND, and it is the form you will use most in later chapters on block size.
How a host uses the mask
A host never needs to know the whole network layout. It uses the mask for one decision when it sends a packet: is the destination on my own subnet?
- Compute
own address AND mask. - Compute
destination address AND mask. - If the two results are equal, the destination is on the same subnet. The host resolves its MAC address with ARP and sends the frame directly.
- If they differ, the host sends the packet to its default gateway.
This is why a wrong mask causes odd failures. Suppose a host at 10.1.5.20 is configured with /24 when the real subnet is /26. It will treat 10.1.5.200 as local and ARP for it. No device on its real segment answers, and the connection fails. The router that could have forwarded the traffic is never asked.
Doing it in Python
Python's integers support & directly, so the host's decision is only a few lines:
import ipaddress
def mask_from_prefix(prefix: int) -> int:
"""Return the 32-bit mask for a prefix length from 0 to 32."""
return (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
def same_subnet(a: str, b: str, prefix: int) -> bool:
mask = mask_from_prefix(prefix)
return (int(ipaddress.ip_address(a)) & mask) == (int(ipaddress.ip_address(b)) & mask)
print(ipaddress.ip_address(mask_from_prefix(20))) # 255.255.240.0
print(same_subnet("10.1.5.20", "10.1.5.60", 26)) # True
print(same_subnet("10.1.5.20", "10.1.5.200", 26)) # FalseThe shift 0xFFFFFFFF << (32 - prefix) produces more than 32 bits, so the & 0xFFFFFFFF trims the result back to 32. Without it, the mask would carry extra high bits.
Validating a mask
Not every dotted-decimal value is a legal mask. 255.0.255.0 has 1s after a 0, so it is not a valid mask. Routers and modern operating systems reject such values. Two common ways to check:
- Invert the mask (flip every bit) and add 1. If the result is a power of two (or 2³² for mask
0.0.0.0), the mask is contiguous. - Convert the mask to a binary string and check that it matches the pattern
1*0*.
The inverted mask also has a name: the wildcard mask. For 255.255.240.0, the wildcard is 0.0.15.255. Wildcard masks appear in access control lists and in some routing protocol configurations on certain router platforms. A wildcard mask is not a subnet mask, so check which one a given command expects.
Common errors
- ANDing decimal values directly. Convert the interesting octet to binary first, or use the block-size shortcut. Decimal intuition fails for values such as 37 AND 240.
- Forgetting which octet is interesting. Only the octet where the mask is neither 255 nor 0 needs calculation. A mask that falls exactly on an octet boundary (
/8,/16,/24) has no such octet. - Reading the mask as a count of hosts. The mask describes which bits are the network. The host count comes from the 0 bits, which is the subject of the next chapter.
- Using a mask that is not contiguous. Some old equipment tolerated these. Current CIDR practice does not.
- Mixing up the two ANDs. The AND of an address with the mask gives the network. The AND of an address with the inverted mask gives the host portion alone.
Checking mask arithmetic with the library
You can confirm a manual result against the standard library without writing the bit logic yourself:
import ipaddress
iface = ipaddress.ip_interface("172.16.37.200/20")
print(iface.netmask) # 255.255.240.0
print(iface.hostmask) # 0.0.15.255
print(iface.network) # 172.16.32.0/20hostmask is the wildcard mask described above. Doing a few calculations by hand and then checking them this way builds the habit of verifying your work.
Exercise
A host at 192.168.10.77 has the mask 255.255.255.192. Using a bitwise AND on the fourth octet, what is its network address? Can it reach 192.168.10.120 directly, without a gateway?
Show answer
77 is 01001101 and 192 is 11000000. The AND is 01000000, which is 64. The network address is 192.168.10.64. For 120 (01111000), the AND with 11000000 is 01000000, which is also 64. The two results match, so 192.168.10.120 is on the same subnet and the host reaches it directly.
Exercise
Convert the prefix /21 to a subnet mask, and then decide whether 255.255.253.0 is a valid mask.
Show answer
/21 has 8 + 8 + 5 ones, so the third octet is 11111000 = 248 and the mask is 255.255.248.0. The value 255.255.253.0 has a third octet of 253 = 11111101. That has a 0 followed by a 1, so the 1s are not contiguous. It is not a valid subnet mask.