Sub topic 1
Reading addresses in binary and dotted decimal
Prerequisites
This chapter assumes you know the following:
- What a computer network and an IP address are, at a basic level.
- Decimal arithmetic and powers of two (1, 2, 4, 8, 16, 32, 64, 128).
- Optional: basic Python, to run the code examples.
Why two notations
A computer stores an IPv4 address as a 32-bit number, which is 32 binary digits (bits). People find 32 digits of 0 and 1 hard to read and easy to mistype. So we write the address as four decimal numbers separated by dots. This is called dotted-decimal notation.
For example, 192.168.1.10 and 11000000.10101000.00000001.00001010 are the same address. The first is for people. The second shows what the hardware works with.
Octets
Each of the four numbers is called an octet, because it stands for 8 bits. Four octets of 8 bits give 4 × 8 = 32 bits.
An octet can hold 2^8 = 256 values, from 0 to 255. That is why 192.168.1.256 is not a valid address, and neither is 300.1.1.1.
Place values
Each bit position in an octet has a place value. Reading from left to right:
| Bit position | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
To turn an octet into decimal, add the place values where the bit is 1. To turn decimal into binary, work from the left. At each position, if the remaining number is at least the place value, write 1 and subtract it. Otherwise write 0.
Worked example
Convert the first octet of 192.168.1.10, which is 192.
- 192 ≥ 128, so write 1. Remainder: 64.
- 64 ≥ 64, so write 1. Remainder: 0.
- All remaining place values are larger than 0, so write 0 for each one.
The result is 11000000. Check it: 128 + 64 = 192.
Doing this for every octet gives the full address:
| Octet | Decimal | Binary |
|---|---|---|
| 1 | 192 | 11000000 |
| 2 | 168 | 10101000 |
| 3 | 1 | 00000001 |
| 4 | 10 | 00001010 |
Always write all 8 bits, padding with leading zeros. The third octet is 00000001, not 1. Padding keeps every octet the same width, so the bit positions line up.
Converting with Python
The format function with the "08b" specifier writes a number as 8 binary digits with leading zeros.
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 code splits the text at each dot, converts each piece to an integer, and formats each integer as 8 bits. Joining the pieces with dots gives the binary form.
The conversion flow
Common errors
- Dropping leading zeros. Writing
1instead of00000001shifts every later bit and breaks the 32-bit layout. - Using values above 255. An octet has only 8 bits, so 255 (
11111111) is the largest value. - Reading binary as decimal. The string
11000000in an address is a bit pattern. Its value is 192, not eleven million. - Adding place values wrongly. Count only the positions that hold a 1. Check the sum against the original number.
Exercises
Exercise
Convert 172.16.5.4 to binary. Write all four octets with 8 bits each.
Show answer
172 = 128 + 32 + 8 + 4, so 10101100. 16 = 00010000. 5 = 00000101. 4 = 00000100. The address is 10101100.00010000.00000101.00000100.
Exercise
What decimal address is 11111111.11111111.11111111.00000000? Why is this value useful to recognize?
Show answer
Each group of eight 1s is 255, and eight 0s is 0, so the address is 255.255.255.0. It is a common subnet mask, which later chapters use to separate the network part from the host part.