Sub topic 1
Reading addresses in binary and dotted decimal
Prerequisites
This chapter assumes you know the following:
- What an IP address is, at the level of "a number that identifies a device on a network".
- How decimal arithmetic and powers of two work. You should know that 2^3 is 8 and 2^8 is 256.
- Basic Python, if you want to run the examples. The code uses only the standard library and simple functions.
No earlier chapter is needed. This is the first chapter, and the rest of the tutorial builds on it.
Why binary matters for addressing
People write IPv4 addresses like 192.168.1.10. Routers and hosts work on the underlying bits. Every later topic in this tutorial depends on those bits: subnet masks, prefix lengths, network and broadcast addresses, and the boundaries of private ranges. If you can move between the two notations quickly, those topics become mechanical.
An IPv4 address is a single 32-bit number. A bit is one binary digit, either 0 or 1. Dotted decimal is a human-friendly way to write those 32 bits. It does not change the value.
Dotted decimal: four octets
Dotted decimal splits the 32 bits into four groups of 8 bits. Each group is an octet, which is 8 bits. Networking texts use "octet" because "byte" once meant different sizes on different machines. Each octet is written as a decimal number, and dots separate the four numbers.
Here is 192.168.1.10 split into octets:
| Octet | Decimal | Binary |
|---|---|---|
| 1st | 192 | 11000000 |
| 2nd | 168 | 10101000 |
| 3rd | 1 | 00000001 |
| 4th | 10 | 00001010 |
Joined together, the address is 11000000.10101000.00000001.00001010. The dots are only a reading aid. The real value is the 32 bits in a row.
An octet has 8 bits, so it can hold 2^8 = 256 different values. Those values run from 0 (00000000) to 255 (11111111). This is why no octet in a valid IPv4 address is above 255. An address such as 192.168.1.300 is invalid.
Place values inside an octet
Binary is a positional system, like decimal. In decimal, each position is worth ten times the one to its right. In binary, each position is worth twice the one to its right. For one octet, the place values from left to right are:
| Bit position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
To read an octet in binary, add the place values wherever the bit is 1. For 00001010, the 1 bits sit under 8 and 2, so the value is 8 + 2 = 10. For 11000000, the 1 bits sit under 128 and 64, so the value is 192.
Memorize this row of eight numbers. Most subnetting arithmetic later in the tutorial reduces to it.
Converting decimal to binary by hand
Use the subtraction method. Go through the place values from 128 down to 1. For each place value, write 1 if it fits into what remains, and subtract it. Otherwise write 0.
Convert 168:
- 128 fits into 168. Write 1. Remainder: 40.
- 64 does not fit into 40. Write 0.
- 32 fits. Write 1. Remainder: 8.
- 16 does not fit. Write 0.
- 8 fits. Write 1. Remainder: 0.
- 4, 2 and 1 do not fit. Write 0, 0, 0.
The result is 10101000, which matches the table above.
Converting binary to decimal
This direction is a single sum. Take 10101100. The 1 bits sit under 128, 32, 8 and 4. The sum is 128 + 32 + 8 + 4 = 172. So 10101100 is 172, the first octet of 172.16.5.4.
Doing the conversion in Python
Python's format function with the "08b" specifier writes a number as binary, padded with zeros to 8 digits. The padding matters. Without it, 1 would print as 1 instead of 00000001, and the octets would no longer line up.
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.00001010This version trusts its input. A more careful version checks that there are exactly four parts, that each part is made of digits, and that each value is at most 255:
def to_binary(address):
parts = address.split(".")
if len(parts) != 4:
raise ValueError(f"expected 4 octets, got {len(parts)}: {address!r}")
octets = []
for part in parts:
if not (part.isascii() and part.isdigit()):
raise ValueError(f"octet is not a decimal number: {part!r}")
value = int(part)
if value > 255:
raise ValueError(f"octet out of range 0-255: {value}")
octets.append(value)
return ".".join(format(octet, "08b") for octet in octets)
def to_dotted_decimal(binary):
parts = binary.split(".")
if len(parts) != 4 or any(len(part) != 8 for part in parts):
raise ValueError("expected four groups of eight bits")
return ".".join(str(int(part, 2)) for part in parts)
print(to_binary("10.0.0.1")) # 00001010.00000000.00000000.00000001
print(to_dotted_decimal("11000000.00000000.00000010.00000001")) # 192.0.2.1The second argument of int(part, 2) tells Python to read the text as base 2. If part contains a character other than 0 or 1, Python raises a ValueError.
The address as one 32-bit number
Because the address is one 32-bit number, you can also write it as an ordinary integer. Each octet is a "digit" in base 256. The first octet is worth 256^3, the second 256^2, the third 256, and the last 1:
def to_integer(address):
a, b, c, d = (int(part) for part in address.split("."))
return a * 256**3 + b * 256**2 + c * 256 + d
print(to_integer("192.168.1.10")) # 3232235786
print(hex(to_integer("192.168.1.10"))) # 0xc0a8010aPrograms and routers store addresses in this form. Longest-prefix matching, which you will meet later, is a comparison of leading bits of this number. The total address space is 2^32 = 4,294,967,296 addresses. That fixed size is the reason IPv4 addresses ran short.
Edge cases and common errors
- Out-of-range octets. Any value above 255 is invalid. Typos such as
192.168.1.256are common in configuration files. - Wrong number of octets.
192.168.1has three parts. Some old tools accept short forms and expand them in surprising ways. Treat these inputs as errors in your own code. - Leading zeros. Is
010decimal 10 or octal 8? Some older parsers, such as the C functioninet_aton, read a leading zero as octal. Python'sipaddressmodule in recent versions rejects such input. Do not write octets with leading zeros in configuration, and do not rely on how a tool treats them. - Dropped padding in binary. Writing
1.10.0.1as1.1010.0.1loses the octet boundaries. Always pad each octet to 8 digits when you work in binary. - Miscounted bits. In long binary strings, mistakes usually come from miscounting positions. Group the bits in fours or eights and say the place values out loud.
What comes next
Now that you can move between the two notations, the next chapter looks at how the 32 bits of an address are organized. Later chapters then divide those bits into a network part and a host part, and every later calculation starts with the conversions you practiced here.
Exercise
Convert 172.16.5.4 to binary by hand, one octet at a time. Then convert 11000000.00000000.00000010.00000001 back to dotted decimal.
Show answer
172 = 128 + 32 + 8 + 4 = 10101100. 16 = 00010000. 5 = 4 + 1 = 00000101. 4 = 00000100. So 172.16.5.4 is 10101100.00010000.00000101.00000100. For the second address, 11000000 = 128 + 64 = 192, 00000000 = 0, 00000010 = 2, and 00000001 = 1. The dotted-decimal form is 192.0.2.1.
Exercise
Why does 192.168.1.256 fail to be a valid IPv4 address, and what is the total number of IPv4 addresses?
Show answer
An octet has 8 bits, so it can hold only the values 0 to 255. The value 256 would need a ninth bit. The address is four octets of 8 bits each, which is 32 bits, so there are 2^32 = 4,294,967,296 possible addresses.
Exercise
The call to_binary("10.0.0.1") returns "00001010.00000000.00000000.00000001". What would the output look like if format(octet, "08b") were replaced with format(octet, "b"), and why is that a problem?
Show answer
It would return "1010.0.0.1". The "b" specifier does not pad, so each octet shows only the bits needed. The octets would no longer be 8 characters wide, which hides the bit positions that masks and prefixes depend on.