Sub topic 1
What an IPv4 address is
Prerequisites
This chapter assumes you know the following:
- What an IP address and a network are, at a basic level.
- How to read and write binary numbers (bits and bytes).
- How to convert between binary and decimal for values 0 to 255.
You do not need any subnetting knowledge yet. This chapter lays the groundwork for it.
The 32-bit structure
An IPv4 address is a 32-bit number that identifies a network interface. The 32 bits are split into four groups of 8 bits. Each group is an octet, which is one byte.
People rarely write the 32 bits as one long string. They use dotted-decimal notation instead. Each octet is converted to a decimal number from 0 to 255, and the four numbers are separated by dots.
| Form | Value |
|---|---|
| Binary | 11000000 10101000 00001010 00000001 |
| Dotted decimal | 192.168.10.1 |
| Single integer | 3232238081 |
All three rows are the same address. The dots are only a reading aid. The device sees one 32-bit value.
Because an octet holds 8 bits, it can take 2^8 = 256 values, from 0 to 255. A value such as 300 in one position is therefore invalid. The whole address space holds 2^32 = 4,294,967,296 addresses.
Seeing the address as a number
Each octet has a place value inside the 32-bit number. The first octet is the most significant. Its bits are worth the largest amounts, and the last octet's bits are worth the smallest. The integer value is:
a × 256^3 + b × 256^2 + c × 256 + d
For 192.168.10.1 that is 192 × 16,777,216 + 168 × 65,536 + 10 × 256 + 1 = 3,232,238,081.
You can confirm this with Python's ipaddress module:
import ipaddress
ip = ipaddress.ip_address("192.168.10.1")
value = int(ip)
print(value) # 3232238081
print(format(value, "032b")) # 11000000101010000000101000000001
print(ipaddress.ip_address(value)) # 192.168.10.1
for octet in ip.packed:
print(f"{octet:3d} = {octet:08b}")
# 192 = 11000000
# 168 = 10101000
# 10 = 00001010
# 1 = 00000001The packed attribute holds the address as four bytes. Iterating over it gives each octet as an integer.
Why the structure matters for subnetting
Subnetting works on individual bits, not on whole octets. A router decides where to send a packet by comparing the leading bits of the destination address with entries in its routing table. Later chapters draw a line somewhere inside the 32 bits. Bits to the left of the line identify the network. Bits to the right identify a host on that network.
That line often falls inside an octet. For example, it can fall after the 26th bit, which is two bits into the fourth octet. You cannot see such a split in dotted decimal. You have to convert the octet to binary first, which is why the next chapter covers binary reading in detail.
Common mistakes
- Using values above 255. An address such as 192.168.1.256 is invalid. Python raises a
ValueErrorfor it. - Adding leading zeros. Some tools read
010as octal 8. Python'sipaddressmodule rejects leading zeros in IPv4 addresses. Write 10, not 010. - Mixing up the integer form and the dotted form. They are the same address in two notations. Subnet arithmetic uses the integer form internally.
- Counting 255 addresses in an octet. The range 0 to 255 holds 256 values.
Exercise
Convert 10.1.2.3 to a 32-bit binary string with dots between the octets, then work out its integer value.
Show answer
Binary: 00001010.00000001.00000010.00000011. Integer: 10 × 16,777,216 + 1 × 65,536 + 2 × 256 + 3 = 167,772,160 + 65,536 + 512 + 3 = 167,838,211.
Exercise
Why can the address 172.16.300.5 never be valid, and how many distinct values can one octet hold?
Show answer
An octet has 8 bits, so it holds 2^8 = 256 values, from 0 to 255. The number 300 does not fit in 8 bits, so the third octet is invalid.