Sub topic 3
Network bits and host bits
What this chapter covers
This chapter assumes you can convert values from 0 to 255 between binary and decimal, and that you know an IPv4 address is 32 bits written as four octets. You do not need to know about masks or CIDR yet. Later chapters formalize both. Here you learn the idea they rest on: every IPv4 address is divided into a network part and a host part, and almost every subnetting calculation comes from where that dividing line sits.
One address, two jobs
An IPv4 address answers two questions at once:
- Which network is this device on?
- Which device is it within that network?
The leftmost bits answer the first question. They are the network bits (also called the network portion or prefix). The remaining rightmost bits answer the second question. They are the host bits. The two parts always add up to 32.
Take 192.168.10.77. Written in binary, it is:
11000000.10101000.00001010.01001101If the first 24 bits are network bits, the split falls like this:
network bits (24) host bits (8)
11000000.10101000.00001010 | 01001101
192.168.10 77Every device whose first 24 bits are 11000000.10101000.00001010 is on the same network. The host bits tell them apart.
The number of network bits is written after a slash, as in 192.168.10.77/24. This is the prefix length. The next chapters cover the notation in full. For now, read /24 as "the first 24 bits are the network part."
Why the split exists
Routers do not keep a route for every individual device. They keep routes for networks. When a packet arrives, the router looks only at the network bits of the destination address and ignores the host bits until the packet reaches the last router. This is the reason the split exists: it lets millions of hosts be reached through a small number of routing entries.
Hosts use the split too. Before sending a packet, a host compares the destination's network bits with its own:
- If they match, the destination is on the same network. The host sends the packet directly, using ARP to find the destination's hardware address.
- If they differ, the host sends the packet to its default gateway, which is a router.
The boundary does not have to fall between octets
In the example above, the boundary sits exactly after the third octet, so you can read the split straight from the dotted-decimal form. That is convenient, but it is a special case. The boundary can fall after any of the 32 bits.
Consider 172.16.37.200 with a 20-bit network part:
| Octet | Decimal | Binary | Role |
|---|---|---|---|
| 1 | 172 | `10101100` | all network |
| 2 | 16 | `00010000` | all network |
| 3 | 37 | `0010 0101` | first 4 bits network, last 4 bits host |
| 4 | 200 | `11001000` | all host |
The third octet is shared between the two parts. The network bits are 10101100.00010000.0010, and the host bits are 0101.11001000. In decimal, the host bits form the value 5 × 256 + 200 = 1480. It is not "200" and not "37." Once the boundary leaves an octet edge, the dotted-decimal digits stop lining up with the two parts, and you have to go to binary to see the split.
Splitting an address in code
The following program converts an address to a 32-character bit string, cuts it at the prefix length, and compares two addresses by their network bits. It uses only the standard library.
def to_bits(address: str) -> str:
"""Return the 32-bit binary string for a dotted-decimal IPv4 address."""
octets = [int(part) for part in address.split(".")]
return "".join(f"{octet:08b}" for octet in octets)
def split(address: str, prefix: int) -> tuple[str, str]:
"""Return (network_bits, host_bits) for an address and prefix length."""
bits = to_bits(address)
return bits[:prefix], bits[prefix:]
def same_network(a: str, b: str, prefix: int) -> bool:
"""True if both addresses share the same first `prefix` bits."""
return to_bits(a)[:prefix] == to_bits(b)[:prefix]
network, host = split("172.16.37.200", 20)
print(network) # 10101100000100000010
print(host) # 010111001000
print(int(host, 2)) # 1480
print(same_network("10.1.1.100", "10.1.1.20", 25)) # True
print(same_network("10.1.1.100", "10.1.1.200", 25)) # False
print(same_network("10.1.1.100", "10.1.1.200", 24)) # TrueThe last three lines show an important behavior. The addresses 10.1.1.100 and 10.1.1.200 share their first three octets, yet they are on different networks when the prefix is 25. The 25th bit is the top bit of the fourth octet. It is 0 for values 0 to 127 and 1 for values 128 to 255. Address 100 falls in the first range and 200 in the second. Move the boundary one bit left to /24, and both addresses are on the same network.
Host bits determine size
If a network has h host bits, those bits can take 2^h different values. That is the number of addresses in the network. The network bits are fixed for every address in it, so only the host bits vary.
| Network bits | Host bits | Addresses in the network |
|---|---|---|
| 8 | 24 | 16,777,216 |
| 16 | 16 | 65,536 |
| 20 | 12 | 4,096 |
| 24 | 8 | 256 |
| 26 | 6 | 64 |
| 30 | 2 | 4 |
Two of these addresses in an ordinary network are not available for hosts. These are the one with all host bits 0 (the network address) and the one with all host bits 1 (the broadcast address). A later chapter on address counts covers them, along with the exceptions for /31 and /32. For now, remember that "addresses in the network" and "addresses you can assign to devices" differ by two in the usual case.
Moving the boundary
The 32-bit total is fixed, so moving the boundary is always a trade:
- Add one network bit, and you take one away from the host side. The network halves in size, and each old network becomes two.
- Remove one network bit, and the network doubles in size, so two adjacent old networks merge into one.
This is the mechanism behind subnetting and summarization. When you "borrow" host bits and treat them as network bits, you create subnets. When you give network bits back, you create a larger block. The 25th bit in the earlier example is a borrowed bit. At /24, the whole range 10.1.1.0 to 10.1.1.255 is one network. At /25, that bit separates it into 10.1.1.0 to 10.1.1.127 and 10.1.1.128 to 10.1.1.255.
Common errors
- Reading the split from the dotted-decimal form only. This works at 8, 16, and 24 bits and fails everywhere else. When the prefix is not a multiple of 8, convert the affected octet to binary.
- Treating the host value as the last octet. At
/20, the host value of172.16.37.200is 1480, as shown above. - Counting from the wrong end. Network bits are the leftmost bits. Prefix length counts from the left.
- Comparing addresses by octets instead of bits. Two addresses with different third octets can still be on the same network, as
172.16.32.1and172.16.47.1are at/20. - Forgetting the reserved pair. A network with h host bits has 2^h addresses, not 2^h usable ones in the standard case.
Practice
Exercise
The address 10.1.1.200 has a 25-bit network part. What is the host-bit value (as a decimal number), and what is the first address of this network?
Show answer
The fourth octet 200 is 11001000. The first bit (1) is the 25th bit of the address, so it is a network bit. The remaining 7 bits, 1001000, are the host bits, which equal 72. Setting the host bits to 0 leaves the fourth octet as 10000000, which is 128. The network's first address is 10.1.1.128.
Exercise
Are 192.168.5.130 and 192.168.5.190 on the same network when the network part is 26 bits? What about 27 bits?
Show answer
The fourth octets are 130 = 10000010 and 190 = 10111110. With 26 bits, the network part includes the first 2 bits of the fourth octet. Both begin with 10, so they are on the same network (range 128 to 191). With 27 bits, the network part includes the first 3 bits. 130 begins with 100 and 190 begins with 101, so they are on different networks (128 to 159 and 160 to 191).