Sub topic 3
Network bits and host bits
This chapter assumes you know what an IP address and a network are. It also assumes you can read binary and convert values from 0 to 255 between binary and decimal. Here you will see how one 32-bit address divides into a network part and a host part. Every later subnetting calculation builds on that division.
The two parts of an address
An IPv4 address has 32 bits. A router needs to know two things about it: which network the address belongs to, and which device on that network it identifies. So the address is split in two:
- The network bits are the leftmost bits. All devices on the same network share the same values here.
- The host bits are the remaining rightmost bits. They tell devices on the same network apart.
The split always falls at a single point. Network bits come first and host bits come last, with no mixing. How many bits are network bits is a choice made by whoever designs the network. The address does not say. A separate value, the prefix length or mask, carries that choice. Later chapters cover those in detail.
A worked example
Take the address 192.168.10.77 and suppose the first 24 bits are network bits.
| Part | Bits | Value |
|---|---|---|
| Address in binary | 11000000.10101000.00001010.01001101 | 192.168.10.77 |
| Network bits (first 24) | 11000000.10101000.00001010 | 192.168.10 |
| Host bits (last 8) | 01001101 | 77 |
Every address from 192.168.10.0 to 192.168.10.255 has the same first 24 bits. They are on the same network. Only the last octet changes, and that octet identifies the host.
Now suppose the split moves to 26 network bits. The last octet is 01001101. Its first two bits, 01, now count as network bits. The host part is the remaining six bits, 001101, which is 13. The network part ends in 01 followed by six zeros, which is 64. So 192.168.10.77 now belongs to the network that starts at 192.168.10.64.
The address is the same. Only the placement of the split changed, and with it the network the address belongs to.
Why the split sets the size of a network
The number of host bits decides how many addresses a network holds. With h host bits there are 2^h combinations, so the network holds 2^h addresses. The number of network bits then equals 32 minus h.
| Network bits | Host bits | Addresses in the network |
|---|---|---|
| 8 | 24 | 16,777,216 |
| 16 | 16 | 65,536 |
| 24 | 8 | 256 |
| 26 | 6 | 64 |
| 30 | 2 | 4 |
Each extra network bit halves the network. Each extra host bit doubles it. This trade-off is the basis of subnetting. You borrow host bits and turn them into network bits to make more, smaller networks.
Checking the split in code
This short program separates an address into its network and host parts for a given number of network bits. It uses the bitwise operations that later chapters explain.
import ipaddress
addr = int(ipaddress.ip_address("192.168.10.77"))
for network_bits in (24, 26):
host_bits = 32 - network_bits
network_part = addr >> host_bits # keep the leftmost bits
host_part = addr & ((1 << host_bits) - 1) # keep the rightmost bits
network_addr = ipaddress.ip_address(network_part << host_bits)
print(network_bits, host_bits, network_addr, host_part)
# 24 8 192.168.10.0 77
# 26 6 192.168.10.64 13Common errors
- Assuming the split must fall on an octet boundary. It can fall anywhere, including in the middle of an octet, as the 26-bit case shows.
- Reading the network part from the dotted-decimal form alone. When the split is inside an octet, convert that octet to binary first.
- Forgetting that the split is not stored in the address. Two devices with the same address bits can be on different networks if they use different split points.
Exercise
The address 10.1.200.130 uses 25 network bits. What are the network part (as the address of the network) and the host part (as a number)?
Show answer
The first three octets, 10.1.200, are 24 bits. The 25th bit is the top bit of the last octet. 130 in binary is 10000010, so that bit is 1. The network bits keep that 1 and clear the other 7 host bits, giving 10000000, which is 128. The network is 10.1.200.128. The host part is the remaining 7 bits, 0000010, which is 2.
Exercise
A network has 6 host bits. How many network bits does it have, and how many addresses does it hold?
Show answer
It has 32 - 6 = 26 network bits. It holds 2^6 = 64 addresses.