Sub topic 2
Subnet Masks and the Bitwise AND
Assume you can read IPv4 addresses, binary numbers, and basic network terms. A subnet mask identifies which bits belong to the network and which bits belong to hosts.
Subnet masks and the bitwise AND
An IPv4 address has 32 bits, written as four decimal octets. A subnet mask also has 32 bits. Mask bits set to 1 identify network bits. Mask bits set to 0 identify host bits.
The bitwise AND compares two bits:
| Address bit | Mask bit | Result |
|---|---|---|
| 1 | 1 | 1 |
| 1 or 0 | 0 | 0 |
For 172.16.37.200/20, the /20 prefix means that the first 20 bits are network bits. The mask is 255.255.240.0. Applying AND gives the network address:
IP address: 172.16.37.200
Mask: 255.255.240.0
Network: 172.16.32.0The mask clears the host bits while keeping the network bits.
Network, broadcast, and usable hosts
The network address identifies the subnet. It cannot normally identify a host.
The broadcast address sets every host bit to 1. It reaches all hosts on the subnet. For 172.16.37.200/20, the host bits cover the range from .32.0 through .47.255, so the broadcast address is 172.16.47.255.
A prefix with h host bits contains 2^h addresses. For ordinary IPv4 subnets, subtract the network and broadcast addresses:
- Total addresses:
2^12 = 4096 - Usable host addresses:
4096 - 2 = 4094 - Usable range:
172.16.32.1to172.16.47.254
Block size and subnet boundaries
The block size is the distance between adjacent network addresses. In the changing octet:
256 - 240 = 16
Therefore, /20 networks begin at multiples of 16 in the third octet: 0, 16, 32, 48, and so on. The value 37 falls in the block beginning at 32.
Splitting a network into equal subnets
Borrowing host bits creates smaller, equal-sized subnets. Splitting a /24 into /26 networks borrows two bits, creating 2^2 = 4 subnets. Each /26 contains 64 addresses.
import ipaddress
network = ipaddress.ip_network("192.168.10.0/24")
for subnet in network.subnets(new_prefix=26):
print(subnet, subnet.network_address, subnet.broadcast_address)Exercise
What are the network and broadcast addresses for 192.168.1.70/26?
Show answer
The block size is 64. The address is in the 192.168.1.64/26 block, so the network is 192.168.1.64 and the broadcast is 192.168.1.127.