Notes › Computer Networks
IP Addressing and Subnetting
IPv4 basics
An IPv4 address is 32 bits, written as 4 numbers (octets) of 8 bits each, 0–255.
192 .168 .1 .10
11000000 10101000 00000001 00001010
Every address has two parts: network part (which network) + host part (which device on it). The subnet mask says where the split is.
Binary place values to remember: 128 64 32 16 8 4 2 1.
Classful addressing (old, still asked)
| Class | First octet | Default mask | Networks / hosts | Use |
|---|---|---|---|---|
| A | 1 – 126 | 255.0.0.0 (/8) | few networks, 16.7 M hosts each | huge orgs |
| B | 128 – 191 | 255.255.0.0 (/16) | 65,534 hosts | medium |
| C | 192 – 223 | 255.255.255.0 (/24) | 254 hosts | small |
| D | 224 – 239 | — | multicast | |
| E | 240 – 255 | — | experimental |
127.x.x.x is reserved for loopback (127.0.0.1 = localhost). Classes wasted addresses, so they were replaced by CIDR.
Private and special addresses
| Range | Meaning |
|---|---|
| 10.0.0.0/8, 172.16.0.0/12 (172.16–172.31), 192.168.0.0/16 | private — used inside homes/offices, not routed on the internet |
| 127.0.0.0/8 | loopback |
| 169.254.0.0/16 | link-local (APIPA — DHCP failed) |
| 0.0.0.0 | "this host" / any address (servers listen on it) |
| 255.255.255.255 | broadcast to the local network |
Subnet mask and CIDR
The mask has 1s for the network bits and 0s for the host bits. CIDR writes how many 1s: /24.
| CIDR | Mask | Hosts per subnet (2ʰ − 2) |
|---|---|---|
| /24 | 255.255.255.0 | 254 |
| /25 | 255.255.255.128 | 126 |
| /26 | 255.255.255.192 | 62 |
| /27 | 255.255.255.224 | 30 |
| /28 | 255.255.255.240 | 14 |
| /29 | 255.255.255.248 | 6 |
| /30 | 255.255.255.252 | 2 (point-to-point links) |
Usable hosts = 2^(host bits) − 2 (the first address is the network address, the last is the broadcast address).
The fast method (block size)
- Find the "interesting" octet — where the mask isn't 255 or 0.
- Block size = 256 − mask value in that octet.
- Networks start at multiples of the block size.
- Network = the multiple at or below the IP; broadcast = next network − 1; hosts in between.
Problem 1: 192.168.10.75/26
- Mask 255.255.255.192 → block = 256 − 192 = 64.
- Networks: .0, .64, .128, .192. 75 falls in .64.
- Network 192.168.10.64, broadcast 192.168.10.127, hosts .65 – .126 (62 hosts).
Problem 2: 10.1.5.200/28
- Mask .240 → block 16. Multiples: …, 192, 208. 200 is in 192.
- Network 10.1.5.192, broadcast 10.1.5.207, hosts .193 – .206 (14 hosts).
Problem 3: 172.16.45.130/20
- /20 = 255.255.240.0 → interesting octet is the 3rd, block 16.
- Multiples: 0, 16, 32, 48. 45 is in 32.
- Network 172.16.32.0, broadcast 172.16.47.255, hosts 172.16.32.1 – 172.16.47.254 (2¹² − 2 = 4094 hosts).
Same-network check
Are 192.168.1.70/26 and 192.168.1.130/26 on the same subnet? Block 64: .70 → .64 network, .130 → .128 network → no, they need a router.
Splitting a network (subnetting)
"Divide 192.168.1.0/24 into 4 equal subnets."
- 4 subnets need 2 borrowed bits (2² = 4) → /24 + 2 = /26, 62 hosts each.
| Subnet | Network | Host range | Broadcast |
|---|---|---|---|
| 1 | 192.168.1.0 | .1 – .62 | .63 |
| 2 | 192.168.1.64 | .65 – .126 | .127 |
| 3 | 192.168.1.128 | .129 – .190 | .191 |
| 4 | 192.168.1.192 | .193 – .254 | .255 |
"Need at least 50 hosts per subnet?" → 2ʰ − 2 ≥ 50 → h = 6 → /26.
VLSM (variable-length subnet masks): give big departments big subnets and small links /30s — allocate the largest first.
Supernetting / route aggregation: combine 192.168.0.0/24 … 192.168.3.0/24 into 192.168.0.0/22 so routers keep one entry.
NAT — Network Address Translation
Your home devices have private IPs (192.168.1.x). The router replaces them with its one public IP when packets leave, remembering which reply goes where using port numbers (PAT / NAT overload). This is a big reason IPv4 hasn't run out completely.
DHCP — automatic IP configuration
New device joins → DORA: Discover (broadcast) → Offer → Request → Acknowledge. It receives an IP, mask, default gateway and DNS server, leased for a time.
IPv6
IPv4 has only ~4.3 billion addresses. IPv6 is 128 bits → 3.4 × 10³⁸ addresses.
2001:0db8:0000:0000:0000:ff00:0042:8329
→ 2001:db8::ff00:42:8329 (drop leading zeros; one run of zero groups → ::)
| IPv4 | IPv6 | |
|---|---|---|
| Size | 32 bits | 128 bits |
| Written as | dotted decimal | hexadecimal with colons |
| Config | manual / DHCP | SLAAC (auto) or DHCPv6 |
| NAT | widely needed | not needed |
| Broadcast | yes | no (multicast instead) |
| Loopback | 127.0.0.1 | ::1 |
| Header checksum | yes | no (faster routing) |
Interview one-liners
- Usable hosts = 2^(32 − prefix) − 2.
- Block size = 256 − mask octet; network = multiple of the block at or below the IP.
- Private ranges: 10/8, 172.16/12, 192.168/16.
- NAT maps many private IPs to one public IP; DHCP hands out IPs automatically (DORA).
- IPv6 = 128-bit addresses, no NAT needed.
- Related: Routing and the Network Layer, OSI and TCP/IP Models.