Sub topic 2
The structure of an IPv4 address
This chapter assumes you know what an IP address is and can do decimal arithmetic with powers of two. The Python examples are optional but run as shown.
A 32-bit number in four pieces
An IPv4 address is a single 32-bit number. People rarely write it as one long number. They split it into four groups of 8 bits, called octets, and write each group in decimal, separated by dots. This is dotted-decimal notation.
Each octet holds 8 bits, so its value runs from 0 to 255. Four octets give 4 × 8 = 32 bits, which allows 2^32 = 4,294,967,296 possible addresses.
| Octet | Bits | Weight of each bit (left to right) |
|---|---|---|
| 1st | 1–8 | 128, 64, 32, 16, 8, 4, 2, 1 |
| 2nd | 9–16 | 128, 64, 32, 16, 8, 4, 2, 1 |
| 3rd | 17–24 | 128, 64, 32, 16, 8, 4, 2, 1 |
| 4th | 25–32 | 128, 64, 32, 16, 8, 4, 2, 1 |
Every octet uses the same bit weights. To read an octet, add the weights of the bits that are set to 1.
Worked example: 192.168.1.10
Convert each octet on its own:
- 192 = 128 + 64, so it is
11000000. - 168 = 128 + 32 + 8, so it is
10101000. - 1 = 1, so it is
00000001. - 10 = 8 + 2, so it is
00001010.
Joined together, the address is 11000000.10101000.00000001.00001010. The dots are only for readability. A router sees the 32 bits as one value.
address = "192.168.1.10"
octets = [int(part) for part in address.split(".")]
binary = ".".join(format(octet, "08b") for octet in octets)
print(binary) # 11000000.10101000.00000001.00001010The format code 08b means "binary, padded with zeros to 8 digits". The padding matters. Without it, 1 would print as 1 instead of 00000001, and the octet would no longer be 8 bits wide.
Where the bits sit
The position of a bit decides how much it counts. Bits on the left of the address are the most significant. Changing the first octet moves the address far away in the number space. Changing the last octet moves it only a little.
That ordering is why networks are described by the leading bits of an address. The next chapter splits the 32 bits into a network part and a host part, and the split always falls at a bit position counted from the left.
Common errors
- Values above 255. An octet has only 8 bits, so
192.168.1.256is not a valid address. The largest octet is11111111, which is 255. - Leading zeros. Some software reads
010as octal, which is 8. Write10, not010. Python'sipaddressmodule rejects leading zeros in IPv4 text. - Fewer than four octets. Some older tools accept shortened forms such as
127.1. Treat these as legacy shorthand and always write all four octets. - Forgetting the padding. When converting by hand or in code, always write 8 digits per octet. A 7-digit octet shifts every bit after it.
Check your understanding
Exercise
Convert 10.0.200.5 to binary, and state how many bits the address has in total.
Show answer
10 = 00001010, 0 = 00000000, 200 = 128 + 64 + 8 = 11001000, 5 = 00000101. The address is 00001010.00000000.11001000.00000101. It has 32 bits in total.
Exercise
Why is 172.16.300.1 not a valid IPv4 address?
Show answer
The third octet, 300, is larger than 255. An octet has 8 bits, so its maximum value is 11111111 in binary, which is 255.