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 a CIDR prefix such as /20 gives the number of network bits in an IPv4 address. Here you will turn a prefix into a subnet mask. You will then use the mask with a bitwise AND to find the network a host belongs to.
What a subnet mask is
A subnet mask is a 32-bit value. Its leading bits are all 1s and its remaining bits are all 0s. The 1s mark the network portion of an address. The 0s mark the host portion.
A prefix length and a mask carry the same information:
| Prefix | Mask in binary | Mask in dotted decimal |
|---|---|---|
| /8 | 11111111.00000000.00000000.00000000 | 255.0.0.0 |
| /16 | 11111111.11111111.00000000.00000000 | 255.255.0.0 |
| /20 | 11111111.11111111.11110000.00000000 | 255.255.240.0 |
| /26 | 11111111.11111111.11111111.11000000 | 255.255.255.192 |
A valid mask never mixes the bits. Something like 255.0.255.0 is not a valid subnet mask, because the 1s must be contiguous.
Converting a prefix to a mask
Split the prefix into whole octets and a remainder.
- Each full group of 8 network bits gives an octet of 255.
- The leftover bits give one partial octet. Use the number of leading 1s to look up the value: 1 bit is 128, 2 is 192, 3 is 224, 4 is 240, 5 is 248, 6 is 252, 7 is 254.
- Every octet after that is 0.
For /20, there are two full octets (16 bits) and 4 leftover bits. The partial octet is 240, so the mask is 255.255.240.0.
The bitwise AND
A bitwise AND compares two numbers bit by bit. The result bit is 1 only if both input bits are 1. ANDing an address with its mask keeps the network bits and clears the host bits. The result is the network address.
Take 172.16.37.200/20. Only the third octet is partial, so work on that one. The other octets pass through unchanged (255) or become 0.
Third octet of address: 37 = 00100101
Third octet of mask: 240 = 11110000
AND result: 32 = 00100000The network address is 172.16.32.0. The host bits of the address were cleared. To get the broadcast address, set all host bits to 1 instead. That gives 172.16.47.255.
Doing it in code
This script repeats the manual method. It builds the mask by shifting, then applies AND and OR.
import ipaddress
ip = int(ipaddress.ip_address("172.16.37.200"))
prefix = 20
mask = (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF
network = ip & mask
broadcast = network | (~mask & 0xFFFFFFFF)
print(ipaddress.ip_address(mask)) # 255.255.240.0
print(ipaddress.ip_address(network)) # 172.16.32.0
print(ipaddress.ip_address(broadcast)) # 172.16.47.255The & 0xFFFFFFFF step matters. Python integers have no fixed width, so without it the shift and the ~ operator would produce values outside 32 bits.
Common errors
- Using the wrong octet. Only one octet is partial. Octets that are 255 keep the address value, and octets that are 0 become 0. Do the AND only on the partial octet.
- Writing a non-contiguous mask. If the binary mask has a 0 followed by a 1, it is invalid.
- Confusing the mask with the address. The mask is not assigned to a host as an address. It only describes how to read an address.
- Forgetting that two hosts are on the same subnet only if their ANDed results match. Compare the network addresses, not the addresses themselves.
Exercise
A host has the address 10.77.130.9 and the mask 255.255.192.0. What are its prefix length, network address, and broadcast address?
Show answer
The mask has 8 + 8 + 2 = 18 network bits, so the prefix is /18. The partial octet is the third one. 130 = 10000010 and 192 = 11000000, so the AND gives 10000000 = 128. The network address is 10.77.128.0. The block size is 256 - 192 = 64, so the block runs from 128 to 191 in the third octet. The broadcast address is 10.77.191.255.
Exercise
Do 192.168.5.70 and 192.168.5.100 share a subnet when the mask is 255.255.255.192?
Show answer
The block size is 256 - 192 = 64. The fourth octet 70 falls in the block 64 to 127. The value 100 also falls in 64 to 127. Both ANDed results are 192.168.5.64, so they share a subnet.