Sub topic 2
Reading addresses in binary
An IPv4 address is a 32-bit number, and every subnetting operation works on those bits. This chapter assumes you know what an IP address and a network are, and that you can count in binary. It covers how to convert each octet quickly and reliably, how to recognize mask patterns, and where conversions usually go wrong.
Why binary matters for subnetting
Dotted-decimal notation is a convenience for people. A router sees one string of 32 bits. The line between the network part and the host part of an address can fall between any two bits, and in decimal that line is often invisible. The address 172.16.37.200 gives no hint that its network part might end in the middle of the third octet. In binary the split is easy to see.
The same applies to subnet masks. The mask 255.255.240.0 looks irregular in decimal. In binary it is a run of 20 ones followed by 12 zeros.
Octets and place values
An octet is a group of 8 bits, written as a decimal number from 0 to 255. An address has four octets, separated by dots. Each bit in an octet has a place value, which is a power of two:
| Bit position (left to right) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
| Power of two | 2^7 | 2^6 | 2^5 | 2^4 | 2^3 | 2^2 | 2^1 | 2^0 |
An octet's decimal value is the sum of the place values where the bit is 1. The octet 11001000 is 128 + 64 + 8 = 200. The largest octet, 11111111, is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. That is why no octet can exceed 255.
Decimal to binary: the subtraction method
Work through the place values from 128 down to 1. At each place value:
- If the remaining number is at least the place value, write 1 and subtract the place value.
- Otherwise, write 0.
Here is the octet 37:
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Remaining before | 37 | 37 | 37 | 5 | 5 | 5 | 1 | 1 |
| Bit | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
The result is 00100101. Check it: 32 + 4 + 1 = 37.
Apply the same steps to each octet of 172.16.37.200:
| Octet | Decimal | Sum of place values | Binary |
|---|---|---|---|
| 1 | 172 | 128 + 32 + 8 + 4 | 10101100 |
| 2 | 16 | 16 | 00010000 |
| 3 | 37 | 32 + 4 + 1 | 00100101 |
| 4 | 200 | 128 + 64 + 8 | 11001000 |
The full address is 10101100.00010000.00100101.11001000.
Binary to decimal
Add the place values wherever a 1 appears. For 11110000, the ones sit under 128, 64, 32 and 16, so the value is 240. With practice you will recognize a common pattern as a single number.
Mask octets: nine values to memorize
A subnet mask is a run of ones followed by a run of zeros, with no gaps. Within a single octet, only nine patterns can appear:
| Decimal | Binary | Ones in the octet |
|---|---|---|
| 0 | 00000000 | 0 |
| 128 | 10000000 | 1 |
| 192 | 11000000 | 2 |
| 224 | 11100000 | 3 |
| 240 | 11110000 | 4 |
| 248 | 11111000 | 5 |
| 252 | 11111100 | 6 |
| 254 | 11111110 | 7 |
| 255 | 11111111 | 8 |
Each value is the previous one plus the next place value: 128 + 64 = 192, 192 + 32 = 224, and so on. Learn this table by heart. You will use it constantly, and it lets you read a mask without any arithmetic.
A mask has at most one octet that is neither 255 nor 0. Every octet to the left of it is 255, and every octet to the right is 0. So 255.255.240.0 is valid: it has 8 + 8 + 4 = 20 ones. The mask 255.255.0.255 is not valid, because a one follows a zero.
Converting with Python
The ipaddress module parses addresses, and Python's format specifier 08b prints a number as 8 binary digits with leading zeros. This script converts in both directions:
import ipaddress
def to_binary(address: str) -> str:
"""Convert dotted decimal to dotted binary, e.g. 10.0.0.1."""
octets = str(ipaddress.IPv4Address(address)).split(".")
return ".".join(f"{int(octet):08b}" for octet in octets)
def to_dotted(binary: str) -> str:
"""Convert dotted binary back to dotted decimal."""
parts = binary.split(".")
if len(parts) != 4 or any(len(p) != 8 for p in parts):
raise ValueError("expected four groups of eight bits")
return str(ipaddress.IPv4Address(".".join(str(int(p, 2)) for p in parts)))
print(to_binary("172.16.37.200")) # 10101100.00010000.00100101.11001000
print(to_binary("255.255.240.0")) # 11111111.11111111.11110000.00000000
print(to_dotted("11000000.10101000.00001010.00000001")) # 192.168.10.1The IPv4Address constructor rejects an octet above 255, so bad input fails early with an error instead of producing a wrong answer.
Treating the address as one 32-bit number
Subnetting operations apply to the whole 32-bit value, not to individual octets. The dots are only a reading aid. Python lets you convert an address to an integer and print it as 32 bits:
import ipaddress
address = ipaddress.IPv4Address("172.16.37.200")
value = int(address)
print(f"{value:032b}")
# 10101100000100000010010111001000
print(ipaddress.IPv4Address(value))
# 172.16.37.200You can use this to check a mask. A valid mask never has a 0 followed directly by a 1:
import ipaddress
def is_valid_mask(mask: str) -> bool:
bits = f"{int(ipaddress.IPv4Address(mask)):032b}"
return "01" not in bits
print(is_valid_mask("255.255.240.0")) # True
print(is_valid_mask("255.255.0.255")) # False
print(is_valid_mask("255.255.250.0")) # FalseThe third mask fails because 250 is 11111010, which has a zero before a one.
Common errors
- Dropping leading zeros. The decimal 5 is
00000101, not101. If you write fewer than 8 bits, the bits shift out of position and every later step is wrong. Always write all 8. - Misplaced place values. The leftmost bit is 128, not 256. A frequent slip is to begin the table at 256 or to repeat a value.
- Octets above 255. An address such as 192.168.256.1 cannot be written in 8 bits. Treat it as an input error.
- Gaps in a mask. Values such as 255.255.255.250 or 255.0.255.0 are not valid masks, even though each octet looks plausible alone.
- Reading across octet boundaries by eye. When a prefix ends partway through an octet, convert only that octet to binary. The other octets are already all ones or all zeros.
- Arithmetic slips in the last step. Always verify by adding the place values back. If the sum does not equal the starting octet, redo the conversion.
What comes next
Reading an address as 32 bits gives you the tool for the next chapter, which marks where the network bits end and the host bits begin. Once you can write both the address and the mask in binary, that split is visible directly.
Exercise
Convert 10.200.130.7 to dotted binary by hand, then convert 255.255.255.224 to binary and state how many network bits the mask contains.
Show answer
10 = 00001010, 200 = 128 + 64 + 8 = 11001000, 130 = 128 + 2 = 10000010, and 7 = 4 + 2 + 1 = 00000111. The address is 00001010.11001000.10000010.00000111. The mask 255.255.255.224 is 11111111.11111111.11111111.11100000, which has 8 + 8 + 8 + 3 = 27 ones, so it contains 27 network bits.
Exercise
Is 255.255.250.0 a valid subnet mask? Explain using binary.
Show answer
No. 250 is 128 + 64 + 32 + 16 + 8 + 2 = 11111010. That octet has a 0 in the sixth position followed by a 1 in the seventh, and a valid mask is a block of ones followed by a block of zeros. The only valid values for a partial octet are 128, 192, 224, 240, 248, 252 and 254.