Sub topic 1
What an IPv4 address is
An IPv4 address is a 32-bit number that identifies a network interface at the IP layer. Every subnetting calculation in this tutorial is arithmetic on that number. Dotted-decimal notation is the human-readable way to write it. This chapter fixes the vocabulary and the mental model that the remaining chapters rely on.
Prerequisites
This chapter assumes you can do the following:
- You know roughly what an IP address and a network are, for example that a laptop and a router each have an address.
- You can read and write binary numbers, and you know that a byte is 8 bits.
- You can convert between binary and decimal for values 0 to 255. Chapter 2 drills this skill, but you need the basics now.
- You can read short Python scripts. The examples use only the standard library.
An address is a 32-bit number
IPv4 addresses are exactly 32 bits long. Each bit is 0 or 1, so the number of possible addresses is 2^32 = 4,294,967,296. That is about 4.3 billion addresses. It is a hard ceiling, and it is why address conservation shaped everything in this tutorial, from CIDR to private ranges.
An address is not a text string, and it does not have inherent parts. Routers and operating systems treat it as an unsigned 32-bit integer. The "parts" you see in notation are a convention that makes the number easier to read and reason about.
The address is assigned to an interface, not to a computer. A router with three interfaces has three addresses, one per connected network. A host with Wi-Fi and Ethernet both active has two. This distinction matters later, because each interface sits in exactly one subnet.
Four octets and dotted decimal
Writing 32 bits in a row is error-prone, so the bits are grouped into four 8-bit groups. Each group is an octet. An octet is 8 bits, so it holds a value from 0 to 255. Each octet is written in decimal, and the four values are joined with dots. This is dotted-decimal notation.
Here is one address in three forms:
| Form | Value |
|---|---|
| Dotted decimal | 192.168.10.77 |
| Binary, grouped by octet | 11000000.10101000.00001010.01001101 |
| 32-bit integer | 3,232,238,157 |
All three describe the same number. The dots are only visual separators and carry no meaning of their own.
How the integer is built
The first octet is the most significant, like the leftmost digit of a decimal number. Its weight is 2^24 = 16,777,216. The second octet's weight is 2^16 = 65,536, the third's is 2^8 = 256, and the fourth's is 1. So:
192 * 16777216 = 3221225472
168 * 65536 = 11010048
10 * 256 = 2560
77 * 1 = 77
----------
3232238157Because the leftmost octet is the most significant, addresses sort numerically the way you read them. 10.0.0.255 is followed by 10.0.1.0, just as 255 is followed by 256 when you count. This counting behavior is what makes ranges of addresses ("every address from 10.0.0.0 to 10.0.0.255") meaningful, and it underlies block sizes in later chapters.
Moving between the three forms in Python
The standard library's ipaddress module converts between the forms. The shifts and masks below show what is happening underneath.
import ipaddress
addr = ipaddress.ip_address("192.168.10.77")
value = int(addr)
print(value) # 3232238157
# Extract each octet by shifting right, then keeping the low 8 bits.
octets = [(value >> shift) & 0xFF for shift in (24, 16, 8, 0)]
print(octets) # [192, 168, 10, 77]
# Show the binary form, one 8-bit group per octet.
print(".".join(f"{o:08b}" for o in octets))
# 11000000.10101000.00001010.01001101
# Go back from the integer to dotted decimal.
print(ipaddress.ip_address(value)) # 192.168.10.77The expression (value >> shift) & 0xFF appears again when we compute subnet masks. It works in two steps. The right shift moves the octet you want into the lowest 8 bits. The AND with 0xFF (binary 11111111) discards everything above them.
Validating dotted decimal by hand
A string is a valid dotted-decimal IPv4 address only if it has exactly four parts, each part is a decimal number from 0 to 255, and the parts are separated by single dots. The following parser applies those rules and builds the integer by shifting.
def parse_ipv4(text):
parts = text.split(".")
if len(parts) != 4:
raise ValueError(f"expected 4 octets, got {len(parts)}: {text!r}")
value = 0
for part in parts:
if not (part.isascii() and part.isdigit()):
raise ValueError(f"octet is not a decimal number: {part!r}")
if len(part) > 1 and part.startswith("0"):
raise ValueError(f"leading zero is ambiguous: {part!r}")
number = int(part)
if number > 255:
raise ValueError(f"octet out of range 0-255: {number}")
value = (value << 8) | number
return value
print(parse_ipv4("10.0.1.5")) # 167772421
for bad in ["192.168.1", "192.168.1.256", "192.168.01.1", "192.168.1.x"]:
try:
parse_ipv4(bad)
except ValueError as err:
print("rejected:", err)Each loop step shifts the running value left by 8 bits to make room for the next octet, then ORs the new octet into the low bits. This is the reverse of the extraction shown earlier.
Common input errors
- Too few or too many octets. "192.168.1" is not a short form of anything you can rely on. Some older tools accept it and interpret it in unexpected ways, so treat it as invalid.
- An octet above 255. Eight bits cannot hold 256. "192.168.1.256" is invalid.
- Leading zeros. Some tools read "010" as octal, which is 8, and others read it as decimal 10. The same text can therefore point at different hosts. Python's
ipaddressmodule rejects leading zeros, and the parser above does too. - Whitespace and stray characters. Strip user input before parsing, and reject anything that is not a digit or a dot.
Addresses are values, but they are used in ranges
On its own, one address says little about where a device sits in a network. The useful structure comes from reading the 32 bits as two parts: a leading group of bits that names the network, and the remaining bits that name a host within it. The two parts together are what routers use to decide where to forward a packet. Chapter 3 develops this split, and CIDR notation in Chapter 5 gives a precise way to say how many leading bits belong to the network.
For now, keep this model: the address is one 32-bit value, and the network is a contiguous range of such values that share the same leading bits. Dotted decimal does not show where the boundary falls. 192.168.10.77 could belong to a network with 256 addresses, one with 64, or one with 4 million. You need extra information (the prefix length or mask) to know which.
A preview of well-known addresses
Some values have fixed meanings that you will meet often. Later chapters treat them in detail.
| Address or range | Meaning |
|---|---|
| 0.0.0.0 | "This host" or "any address", depending on context |
| 127.0.0.0 to 127.255.255.255 | Loopback, traffic that stays on the local host (127.0.0.1 is the usual one) |
| 10.0.0.0 to 10.255.255.255 | Private use (RFC 1918) |
| 172.16.0.0 to 172.31.255.255 | Private use (RFC 1918) |
| 192.168.0.0 to 192.168.255.255 | Private use (RFC 1918) |
| 169.254.0.0 to 169.254.255.255 | Link-local, self-assigned when no DHCP server answers |
| 224.0.0.0 to 239.255.255.255 | Multicast |
| 255.255.255.255 | Limited broadcast, to all hosts on the local network |
Not every one of the 4.3 billion values can be given to an ordinary host. Some are reserved, so the number of usable host addresses is always somewhat smaller than the raw count.
Checking the structure with the library
You can confirm properties of an address without doing any arithmetic yourself. These attributes are useful when you are debugging a plan.
import ipaddress
for text in ["192.168.10.77", "127.0.0.1", "169.254.3.9", "8.8.8.8", "224.0.0.5"]:
a = ipaddress.ip_address(text)
print(
f"{text:<14} version={a.version} private={a.is_private} "
f"loopback={a.is_loopback} link_local={a.is_link_local} "
f"multicast={a.is_multicast}"
)
print(ipaddress.ip_address("10.0.0.255") + 1) # 10.0.1.0
print(ipaddress.IPV4LENGTH) # 32Adding 1 to an address moves to the next integer, and the octets carry over like decimal digits. This is the same behavior that made "10.0.0.255 then 10.0.1.0" natural above.
Summary of the working model
- An IPv4 address is an unsigned 32-bit integer, written as four octets in dotted decimal.
- Each octet is 8 bits and holds 0 to 255. The leftmost octet is the most significant.
- The total space is 2^32 addresses, and some of them are reserved.
- Dotted decimal does not show where the network ends and the host begins. A prefix length or mask supplies that, and the next chapters build on it.
- Validate input numerically, and reject leading zeros and out-of-range octets.
Exercise
Convert 10.0.1.5 to its 32-bit integer value, showing the weight of each octet. Then say whether "172.16.5.300" is a valid address and why.
Show answer
10 16,777,216 = 167,772,160; 0 65,536 = 0; 1 256 = 256; 5 1 = 5. The sum is 167,772,421. "172.16.5.300" is not valid, because the last octet is 300 and an octet is 8 bits, so its maximum value is 255.
Exercise
A script reads the address "192.168.010.1" and one tool treats the third octet as octal. Which decimal value does that tool get for the third octet, and why is this a reason to reject leading zeros?
Show answer
Octal 010 equals 8 in decimal, so the tool reads 192.168.8.1. A tool that reads the text as decimal would get 192.168.10.1. The same text would point at two different hosts, so strict parsers reject leading zeros.