Learn › Part 1 ยท Networking foundations
IPv4 addressing, subnetting and CIDR
Classful addressing, casting types, subnet masks, bitwise AND routing, CIDR validity rules and worked subnetting.
IPv4 basics
An IPv4 address is 32 bits, written as four octets, giving 232 ≈ 4.29 billion addresses. Every address splits into a network ID (used by routers) and a host ID (the interface inside that network).
Classful addressing (1981–1993)
The space was split by successive halving on the leading bits: 232 = 231 (A) + 230 (B) + 229 (C) + 228 (D) + 228 (E).
| Class | Leading bits | First octet | Default mask | Networks | Usable hosts each |
|---|---|---|---|---|---|
| A | 0 | 0–127 | /8 | 27 = 128 | 224 − 2 = 16,777,214 |
| B | 10 | 128–191 | /16 | 214 = 16,384 | 65,534 |
| C | 110 | 192–223 | /24 | 221 = 2,097,152 | 254 |
| D | 1110 | 224–239 | Multicast | ||
| E | 1111 | 240–255 | Experimental | ||
Casting and special addresses
- Unicast: one sender to one receiver.
- Limited broadcast 255.255.255.255: every host on the local network; routers never forward it.
- Directed broadcast: a remote network's ID with all host bits set to 1 (e.g. 20.255.255.255 for 20.0.0.0/8); routed to that network, then broadcast there.
- Network address: all host bits 0; identifies the network and is not assigned to a host.
Subnetting formulas
Borrow n host bits ⇒ 2n subnets. With h host bits left: 2h addresses, 2h − 2 usable hosts.
Network ID = destination IP AND mask. Broadcast = network ID with all host bits set to 1.
Worked example: four departments from 192.168.1.0/24
Need 4 subnets ⇒ 22 = 4, borrow 2 bits ⇒ /26, mask 255.255.255.192, 6 host bits ⇒ 64 addresses, 62 usable.
| Bits | Network | Hosts | Broadcast |
|---|---|---|---|
| 00 | 192.168.1.0 | .1–.62 | .63 |
| 01 | 192.168.1.64 | .65–.126 | .127 |
| 10 | 192.168.1.128 | .129–.190 | .191 |
| 11 | 192.168.1.192 | .193–.254 | .255 |
A packet for 192.168.1.70: 70 = 01000110, 192 = 11000000, AND = 01000000 = 64 ⇒ network 192.168.1.64, so it leaves on the second subnet's interface. Routers store networks, not hosts, which is why they mask instead of looking up the host address.
CIDR
Classless Inter-Domain Routing (1993) allows any prefix from /1 to /32, so blocks fit the need (an organisation needing 1,000 hosts gets a /22 of 1,024 addresses instead of a Class B of 65,536). A range is a valid CIDR block only if:
- the addresses are contiguous;
- the count is a power of two (2n);
- the first address is divisible by the block size (all host bits zero).
Worked example
100.1.2.32–100.1.2.47: 16 addresses = 24, and 32 / 16 = 2 exactly ⇒ valid, 100.1.2.32/28. By contrast 100.1.2.40–.55 also holds 16 addresses but 40 is not a multiple of 16, so it is not a block.
Aggregation follows the same rules: 200.1.4.0/24 to 200.1.7.0/24 summarise to 200.1.4.0/22, but 200.1.2.0 to 200.1.5.0 cannot, because 2 is not a multiple of 4.
Common pitfalls
- Forgetting the −2 for network and broadcast addresses.
- Assuming octet boundaries: in 10.45.77.200/20 the network is 10.45.64.0, not 10.45.77.0.
- Counting subnets from host bits (or the reverse): a /24 holds 16 /28 subnets of 14 usable hosts each.
- In VLSM, allocate the largest subnets first so blocks stay aligned and do not overlap.