Sub topic 2
Reading addresses in binary
This chapter assumes you can convert single values from 0 to 255 between binary and decimal. It shows how to apply that skill to whole IPv4 addresses and masks, which is the basis for every subnet calculation that follows.
Four octets, 32 bits
An IPv4 address is a 32-bit number. It is written as four octets, which are groups of 8 bits. Each octet is shown in decimal and separated by dots. The dots are only a display convention. The router sees one continuous string of 32 bits.
Each octet has eight bit positions. Each position has a place value, and a bit set to 1 adds that value.
| Bit position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
The largest octet is 11111111, which is 255. The smallest is 00000000, which is 0. This is why no octet can exceed 255.
Converting by subtraction
To convert a decimal octet to binary, go through the place values from left to right. Write 1 if the value fits in what remains, subtract it, and write 0 otherwise.
Take 200:
- 128 fits, so write 1. Remainder 72.
- 64 fits, so write 1. Remainder 8.
- 32 and 16 do not fit, so write 0 and 0.
- 8 fits, so write 1. Remainder 0.
- 4, 2 and 1 do not fit, so write 000.
The result is 11001000. To go back, add the place values of the 1 bits: 128 + 64 + 8 = 200.
A full address
Here is 172.16.37.200 converted one octet at a time:
| Octet | Decimal | Binary |
|---|---|---|
| 1 | 172 | 10101100 |
| 2 | 16 | 00010000 |
| 3 | 37 | 00100101 |
| 4 | 200 | 11001000 |
Written out, the address is 10101100.00010000.00100101.11001000. Always pad each octet to 8 digits. Writing 37 as 100101 shifts every later bit and gives a wrong answer.
Masks look different in binary
A subnet mask is also 32 bits, and it is always a run of 1s followed by a run of 0s. So 255.255.255.192 is:
11111111.11111111.11111111.11000000
Because the 1s are contiguous, only nine values can appear in an octet of a valid mask: 0, 128, 192, 224, 240, 248, 252, 254 and 255. A mask such as 255.255.255.100 is invalid, because 100 is 01100100 and the 1s are not contiguous.
Checking with code
This script converts in both directions, so you can check manual work.
def to_binary(addr):
return ".".join(f"{int(octet):08b}" for octet in addr.split("."))
def to_decimal(bits):
return ".".join(str(int(octet, 2)) for octet in bits.split("."))
print(to_binary("172.16.37.200"))
# 10101100.00010000.00100101.11001000
print(to_binary("255.255.255.192"))
# 11111111.11111111.11111111.11000000
print(to_decimal("11000000.10101000.00001010.00100101"))
# 192.168.10.37The format specifier 08b prints a number in binary and pads it with leading zeros to 8 digits. int(octet, 2) parses a binary string.
Common errors
- Dropping leading zeros, so an octet has fewer than 8 digits.
- Converting the whole address as one decimal number instead of four separate octets.
- Mixing up place values, for example reading the leftmost bit as 1 instead of 128.
- Accepting a mask whose 1s and 0s are interleaved.
Exercise
Write 198.51.100.130 in binary, and then give the decimal form of the mask 11111111.11111111.11111000.00000000.
Show answer
198 = 11000110, 51 = 00110011, 100 = 01100100, 130 = 10000010, so the address is 11000110.00110011.01100100.10000010. In the mask, the third octet 11111000 is 128 + 64 + 32 + 16 + 8 = 248, so the mask is 255.255.248.0.