IPv4 Addressing, Subnet Masks, and CIDR
IPv4
An IPv4 address is 32 bits (4 bytes) long and is usually written in dotted-decimal notation. For example, 11000001 00100000 11011000 00001001 is written in dotted-decimal notation as 193.32.216.9.
Dotted-decimal notation splits the 32 bits into 4 bytes, writes each byte as a decimal number from 0 to 255, and separates them with dots:
| Binary | 11000001 | 00100000 | 11011000 | 00001001 |
|---|---|---|---|---|
| Decimal | 193 | 32 | 216 | 9 |
An IP address consists of a network part (subnet part) and a host part. IP addresses with the same network part belong to the same network segment.
Strictly speaking, an IP address identifies an interface of a host or router, not the device itself. A host usually has one interface; a router connects several links, and each of its interfaces has its own IP address. Interfaces on the same network segment can communicate directly without passing through a router.
Address Classes
IP addresses are divided into five classes.
Class A has networks (the all-0 and all-1 network numbers are reserved), and each network can hold hosts.
Class B has networks, and each network can hold hosts.
Class C has networks, and each network can hold hosts.
Class D addresses are used for multicast; a multicast address addresses a group of computers at once.
Class E addresses are reserved for future use and serve only experimentation and development.
The first byte's decimal value also identifies the class quickly: class A is 1–126, class B 128–191, class C 192–223, class D 224–239, and class E 240–255. In every network, the address whose host part is all 0s denotes the network itself and the address whose host part is all 1s is that network's broadcast address, so the number of assignable hosts is always reduced by 2.
Some IP address ranges are designated by international convention for local networks and are not used on the public Internet:
- 10.0.0.0 10.255.255.255
- 172.16.0.0 172.31.255.255
- 192.168.0.0 192.168.255.255
There are also special addresses: 127.0.0.0/8 is used for loopback testing on the local host (for example, 127.0.0.1), 0.0.0.0 means "this host" (used before a host knows its address), and 255.255.255.255 is the limited broadcast address on the local link.
KNOWLEDGE QUIZ
Subnet Masks
The Internet's current address-assignment strategy is Classless Interdomain Routing (CIDR). An address has the form a.b.c.d/x, where the x most significant bits form the network part of the address.
This allows each class of addresses to be divided into smaller networks, significantly improving the efficiency of address allocation and effectively easing the shortage of IP addresses. For example, 200.23.16.0/23 means that the first 23 bits of the IPv4 address are the network part.
A prefix length /x can also be written as a subnet mask whose first x bits are 1 and whose remaining bits are 0. For example, /23 corresponds to:
ANDing an address with its subnet mask bit by bit yields the network address. The host part has bits, and the all-0 and all-1 values cannot be assigned to hosts:
| Item | 200.23.16.0/23 |
|---|---|
| Subnet mask | 255.255.254.0 |
| Network address (host bits all 0) | 200.23.16.0 |
| Broadcast address (host bits all 1) | 200.23.17.255 |
| Assignable addresses | 200.23.16.1 – 200.23.17.254 |
| Assignable hosts |
Hosts use the mask before sending a datagram as well. A host ANDs both its own address and the destination address with its mask. If the network addresses match, it delivers directly on the local link; otherwise it sends the datagram to its default router (default gateway).
Flip bits and drag the network/host boundary
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
KNOWLEDGE QUIZ
Subnetting
An organization that receives an address block can borrow high-order host bits to divide it into smaller subnets. Each additional prefix bit splits a block in half. Different subnets can use different prefix lengths, which is called variable-length subnet masking (VLSM). To choose a prefix for a subnet that needs hosts:
A subnet with 200 hosts therefore needs at least a /24 (254 usable addresses), and one with 20 hosts needs at least a /27 (30 usable addresses). Allocation usually starts with the largest requirement so that small subnets do not fragment the space and leave no contiguous room for large ones.
Carve one /23 into subnets for four departments
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Find the subnet of 192.168.10.77/26
A host is configured as 192.168.10.77/26. Find its subnet mask in dotted-decimal, network address, broadcast address, usable host range, and number of usable hosts. Is 192.168.10.130 on the same subnet?
Decide in Python whether two hosts share a subnet
Without using the ipaddress module, write Python that converts dotted-decimal addresses to 32-bit integers, applies a prefix mask, and prints, for each pair, both network addresses and whether the hosts share a subnet. Use these pairs: 192.168.10.77 & 192.168.10.100 (/26), 192.168.10.77 & 192.168.10.130 (/26), 200.23.17.5 & 200.23.16.200 (/23).