IPv4, classful model, CIDR, and subnet calculation
We will analyze how a 32-bit IPv4 address is divided into network and host parts. In the simulators, you can calculate the range, compare two hosts, verify a mask, and distribute one address block among several subnets.
IPv4 structure, bitwise formulas, and Horner's method
Address IPv4 — is 32 bits (4 bytes = 4 octets, each from 0 to 255). A notation with an octet like 212.260.9.11 is invalid because 260 does not fit into 8 bits! The integer 32-bit representation of an address can be calculated via Horner's scheme:
Network address = IP AND Mask | Broadcast = Network OR (NOT Mask) | Hosts = 232 − p − 2
- Why do we subtract 2? The very first subnet address (all host bits =
0) denotes the network itself, and the last one (all host bits =1) is the broadcast address broadcast. They cannot be assigned to regular hosts. - Exceptions:
/31/31 (RFC 3021) — two addresses for a direct point-to-point link without broadcast;/32/32 — a route to one specific node.
Valid subnet masks and CIDR
In a correct 32-bit mask, there are first consecutive ones (network part, prefix length /p), then consecutive zeros (host part). Therefore, each mask octet can take exactly one of 9 values:
248 (11111000) · 252 (11111100) · 254 (11111110) · 255 (11111111)
255.255.250.0 is invalid (250 = 111110102 — a 0 appeared inside the ones!); mask 255.254.128.0 is also invalid (zeros already started in the 2nd octet, so the 3rd octet must be 0) 0!).CIDR (Classless Inter-Domain Routing) allows shifting the boundary to any bit, not just by octets (/8, /16, /24). For example, for ≥ 4000 hosts, we take 12 host bits (212^12 - 2 = 4094) → prefix /20 /20 (255.255.240.0).
Historical IPv4 classes and checking "are they in the same subnet?"
| Class | Leading bits | 1st octet | Networks / Mask | Hosts in network |
|---|---|---|---|---|
| A | 0... | 1–126 (0 and 127 are special) | 126 networks · /8 | 224 − 2 = 16 777 214 |
| B | 10... | 128–191 | 16,384 networks · /16 | 216 − 2 = 65 534 |
| C | 110... | 192–223 | 2,097,152 networks · /24 | 28 − 2 = 254 |
| D | 1110... | 224–239 | — | Multicast |
| E | 1111... | 240–255 | — | Reserved (experiments) |
Special, private (RFC 1918) addresses and a look at IPv6
| Range / Address | Name and role |
|---|---|
10.0.0.0/8 · 172.16.0.0/12 · 192.168.0.0/16 | Private (Private, RFC 1918) — local networks, not routed on the public Internet (exit via NAT) |
127.0.0.0/8 (usually 127.0.0.1) | Loopback — loopback inside the device itself for testing the stack and local services |
169.254.0.0/16 | Link-Local (APIPA, RFC 3927) — auto-configuration in Windows, macOS, and Linux if the DHCP server does not respond |
100.64.0.0/10 | CGNAT (RFC 6598) — provider's shared address space for carrier-grade NAT (between the client and the public Internet) |
224.0.0.0/4 (224.0.0.0–239.255.255.255) | Multicast — delivery to a group of subscribed recipients |
255.255.255.255 | Limited broadcast — to all nodes in the local segment, never goes beyond the router |
0.0.0.0 / 0.0.0.0/0 | Unspecified address (or "all interfaces") / default route |
2001:db8::1; ::1 — loopback, fe80::/10 — link-local, 2000::/3 2001:db8::/32 — global addresses), supports autoconfiguration SLAAC, simplifies the header and eliminates the need for mandatory NAT.