IPv4
How Internet Addressing Works
LinkedIn Hook
Every time your browser loads a page, a 32-bit number quietly does the impossible.
It identifies your device among billions of machines on the planet. It tells every router along the way exactly where to send your data. And it does all of this in the time it takes a packet to travel across a continent.
That number is an IPv4 address — and most developers have been using them for years without understanding what those four numbers actually mean.
Here is what every software engineer needs to know:
- A 32-bit address has two parts: the network portion and the host portion — separated by the subnet mask
- Private ranges (
10.x.x.x,172.16.x.x,192.168.x.x) never appear on the public internet — they are reserved for internal use- 127.0.0.1 is loopback — a packet sent here never leaves your machine
- Classes A, B, C divide the address space by the size of the network — and knowing them explains why your office uses
192.168.x.xand not10.x.x.xIf you have ever seen a 192.168.1.1 in your router config and wondered why — this lesson explains exactly that.
Full lesson → [link]
#Networking #IPv4 #BackendEngineering #SystemDesign #InterviewPrep
What You'll Learn
- What a 32-bit IPv4 address is and how dotted-decimal notation works
- How the subnet mask divides every address into a network portion and a host portion
- IPv4 address classes (A, B, C, D, E) and what each range is used for
- The private IP ranges and why they exist (RFC 1918)
- Special addresses: loopback, broadcast, and APIPA
The Analogy That Makes This Click
Think of an IPv4 address as a postal address — but one where the format is strictly enforced.
- The network portion is like the city and zip code. It tells the postal system which city (network) this address belongs to. Routers use only this part to decide where to send the packet — they do not care about the house number at this stage.
- The host portion is like the street address and house number. It identifies the specific device within that city (network). Only the final router — the one connected to that network — uses this part.
A postal worker routing a letter from New York to Chicago does not read the street address. They read the city. Only the local mail carrier in Chicago reads the specific street. IPv4 routing works exactly the same way.
The 32-Bit Structure
An IPv4 address is a 32-bit binary number. That is its true form. Every IPv4 address you have ever seen is just a human-readable representation of those 32 bits.
Dotted-Decimal Notation
To make 32-bit addresses readable, they are written as four octets (8-bit groups), each converted to decimal, separated by dots:
Binary: 11000000 . 10101000 . 00000001 . 01101001
Decimal: 192 . 168 . 1 . 105
Each octet is 8 bits, so each one ranges from 0 to 255. That gives you:
0.0.0.0 → minimum
255.255.255.255 → maximum (broadcast)
The total address space: 2³² = 4,294,967,296 addresses (~4.3 billion).
Why 4.3 Billion Is Not Enough
In 1981 when IPv4 was designed, 4.3 billion addresses sounded infinite. Nobody anticipated:
- Every smartphone needing its own address
- IoT devices (thermostats, cameras, sensors) proliferating into the billions
- Cloud providers needing hundreds of thousands of addresses per region
By the early 2010s, IANA (the global authority) exhausted its pool of unallocated IPv4 blocks. The solution in the short term: NAT (Network Address Translation). The long-term solution: IPv6 (covered in Lesson 3.2).
The Subnet Mask
An IPv4 address alone does not tell you where the network ends and the host begins. That is what the subnet mask does.
A subnet mask is also a 32-bit number. It has a block of consecutive 1-bits on the left (the network portion) and 0-bits on the right (the host portion):
255.255.255.0 in decimal
11111111.11111111.11111111.00000000 in binary
How the Mask Works
You apply the subnet mask to an IP address using a bitwise AND operation. The result is the network address — the identifier of the network this IP belongs to:
IP address: 192.168.1.105 → 11000000.10101000.00000001.01101001
Subnet mask: 255.255.255.0 → 11111111.11111111.11111111.00000000
─────────────────────────────────────
Network addr: 192.168.1.0 → 11000000.10101000.00000001.00000000
The host portion is what remains after masking — in this case, 105 (the last octet).
IPv4 Address Classes
Before CIDR (Classless Inter-Domain Routing, covered in Lesson 3.4) was introduced in 1993, IPv4 addresses were divided into fixed classes. You will still see class terminology in interviews and legacy documentation.
| Class | First Octet Range | Default Subnet Mask | Network Bits | Host Bits | Networks | Hosts per Network |
|---|---|---|---|---|---|---|
| A | 1 – 126 | 255.0.0.0 (/8) | 8 | 24 | 128 | 16,777,214 |
| B | 128 – 191 | 255.255.0.0 (/16) | 16 | 16 | 16,384 | 65,534 |
| C | 192 – 223 | 255.255.255.0 (/24) | 24 | 8 | 2,097,152 | 254 |
| D | 224 – 239 | N/A | — | — | — | Multicast only |
| E | 240 – 255 | N/A | — | — | — | Reserved/Research |
Class A was given to large organizations (IBM got 9.0.0.0/8, MIT got 18.0.0.0/8). Class C is what most small networks use — your home router likely lives in a 192.168.x.x Class C range. Class D is for multicast (one-to-many delivery). Class E is reserved.
The class system was inefficient — a company that needed 300 addresses had to take a full Class B (65,534 addresses) and waste most of them. CIDR replaced classful addressing with flexible prefix lengths.
Private IP Ranges (RFC 1918)
Three blocks of IPv4 address space are reserved for private use — they are never routed on the public internet:
| Range | CIDR Block | Class | Common Use |
|---|---|---|---|
10.0.0.0 – 10.255.255.255 | 10.0.0.0/8 | A | Large enterprise networks, cloud VPCs |
172.16.0.0 – 172.31.255.255 | 172.16.0.0/12 | B | Medium networks, Docker default bridge |
192.168.0.0 – 192.168.255.255 | 192.168.0.0/16 | C | Home routers, small offices |
These addresses are defined in RFC 1918. Any device can use them internally, but packets with these source addresses are dropped at the internet boundary — they mean nothing to public routers.
Why they exist: NAT (Network Address Translation) allows thousands of devices with private IPs to share a single public IP. Your home router has one public IP from your ISP. Every device on your home network has a private 192.168.x.x address. When your machine sends a request to Google, the router rewrites the source IP to its public IP, forwards the packet, and maps the response back to your device. Without private ranges and NAT, IPv4 would have run out decades earlier.
Special-Purpose Addresses
Loopback — 127.0.0.1
The entire 127.0.0.0/8 block is reserved for loopback. A packet sent to any address in this range never leaves the machine — it loops back to the local network stack.
127.0.0.1 is the standard loopback address, commonly called localhost. It is used to test local servers, connect to a database on the same machine, or verify that the network stack is functioning.
# Test that your local web server is running
curl http://127.0.0.1:3000
# Same as above — "localhost" resolves to 127.0.0.1 via /etc/hosts
curl http://localhost:3000
Broadcast — 255.255.255.255
The limited broadcast address — a packet sent here is delivered to all devices on the local network. Routers do not forward broadcast packets. Used by DHCP discovery (your machine broadcasts "Who is the DHCP server?" when it first connects).
APIPA — 169.254.x.x
Automatic Private IP Addressing — the 169.254.0.0/16 range. When a device cannot reach a DHCP server to get an IP address, it self-assigns an address in this range. If you see a 169.254.x.x address on a machine, it almost always means the DHCP server is unreachable — a useful diagnostic signal.
Code Example 1 — IPv4 Address Parser and Classifier
This Node.js example parses a dotted-decimal IPv4 address, converts it to binary, applies a subnet mask, and classifies the address type.
// IPv4 address parser, classifier, and network calculator
function parseIPv4(ipString) {
const octets = ipString.split(".").map(Number);
if (octets.length !== 4 || octets.some(o => isNaN(o) || o < 0 || o > 255)) {
throw new Error(`Invalid IPv4 address: ${ipString}`);
}
return octets;
}
function toBinary(octets) {
return octets
.map(o => o.toString(2).padStart(8, "0"))
.join(".");
}
function toUint32(octets) {
return ((octets[0] << 24) | (octets[1] << 16) | (octets[2] << 8) | octets[3]) >>> 0;
}
function classifyAddress(octets) {
const first = octets[0];
// Special addresses
if (first === 127) return "Loopback (127.0.0.0/8)";
if (first === 169 && octets[1] === 254) return "APIPA / Link-Local (169.254.0.0/16)";
if (first >= 224 && first <= 239) return "Class D — Multicast";
if (first >= 240) return "Class E — Reserved";
// RFC 1918 private ranges
if (first === 10) return "Private — Class A (10.0.0.0/8)";
if (first === 172 && octets[1] >= 16 && octets[1] <= 31) return "Private — Class B (172.16.0.0/12)";
if (first === 192 && octets[1] === 168) return "Private — Class C (192.168.0.0/16)";
// Public addresses
if (first >= 1 && first <= 126) return "Public — Class A";
if (first >= 128 && first <= 191) return "Public — Class B";
if (first >= 192 && first <= 223) return "Public — Class C";
return "Unknown";
}
function applyMask(ipOctets, maskOctets) {
const ipInt = toUint32(ipOctets);
const maskInt = toUint32(maskOctets);
const networkInt = (ipInt & maskInt) >>> 0;
const broadcastInt = (networkInt | (~maskInt >>> 0)) >>> 0;
const toOctets = n => [
(n >>> 24) & 0xff,
(n >>> 16) & 0xff,
(n >>> 8) & 0xff,
n & 0xff,
];
return {
network: toOctets(networkInt).join("."),
broadcast: toOctets(broadcastInt).join("."),
firstHost: toOctets(networkInt + 1).join("."),
lastHost: toOctets(broadcastInt - 1).join("."),
hostCount: broadcastInt - networkInt - 1,
};
}
// === Demo ===
const examples = [
{ ip: "192.168.1.105", mask: "255.255.255.0" },
{ ip: "10.0.42.7", mask: "255.0.0.0" },
{ ip: "172.16.5.200", mask: "255.255.0.0" },
{ ip: "8.8.8.8", mask: "255.255.255.0" },
{ ip: "127.0.0.1", mask: "255.0.0.0" },
];
for (const { ip, mask } of examples) {
const ipOctets = parseIPv4(ip);
const maskOctets = parseIPv4(mask);
const info = applyMask(ipOctets, maskOctets);
console.log(`\n=== ${ip} / ${mask} ===`);
console.log(` Binary: ${toBinary(ipOctets)}`);
console.log(` Type: ${classifyAddress(ipOctets)}`);
console.log(` Network: ${info.network}`);
console.log(` Broadcast: ${info.broadcast}`);
console.log(` Host range: ${info.firstHost} – ${info.lastHost}`);
console.log(` Host count: ${info.hostCount}`);
}
What this shows:
- An IPv4 address is just a 32-bit integer — the dotted-decimal format is only for human readability
- The subnet mask applied via bitwise AND extracts the network address
- Inverting the mask and ORing gives the broadcast address
- Host count = broadcast address − network address − 1 (the −1 subtracts the network and broadcast addresses, which cannot be assigned to hosts)
Code Example 2 — Simple DHCP Discovery Simulation
This example simulates what happens when a device first joins a network and needs an IP address from a DHCP server.
// Simplified DHCP (Dynamic Host Configuration Protocol) simulation
// Shows: DISCOVER → OFFER → REQUEST → ACK
class DHCPServer {
constructor(networkBase, subnetMask, rangeStart, rangeEnd) {
this.networkBase = networkBase;
this.subnetMask = subnetMask;
this.gateway = networkBase.replace(/\d+$/, "1");
this.dns = "8.8.8.8";
this.leases = new Map(); // mac → ip
this.pool = this.buildPool(networkBase, rangeStart, rangeEnd);
}
buildPool(base, start, end) {
const prefix = base.split(".").slice(0, 3).join(".");
const pool = [];
for (let i = start; i <= end; i++) {
pool.push(`${prefix}.${i}`);
}
return pool;
}
// Client broadcasts DHCPDISCOVER — "I need an IP, is there a server?"
discover(clientMAC) {
console.log(`\n[DHCP] DISCOVER from ${clientMAC} (broadcast: 255.255.255.255)`);
// Server responds with DHCPOFFER — "I can give you this IP"
const available = this.pool.find(ip => !this.leases.has(ip));
if (!available) {
console.log(`[DHCP] OFFER rejected — pool exhausted`);
return null;
}
console.log(`[DHCP] OFFER → ${clientMAC} offered ${available}`);
return available;
}
// Client broadcasts DHCPREQUEST — "I accept the offer from server X"
request(clientMAC, offeredIP) {
console.log(`[DHCP] REQUEST from ${clientMAC} for ${offeredIP}`);
// Server responds with DHCPACK — "Confirmed, the lease is yours"
this.leases.set(offeredIP, { mac: clientMAC, leaseTime: 86400 }); // 24h lease
console.log(`[DHCP] ACK → Lease granted: ${offeredIP} to ${clientMAC}`);
return {
ip: offeredIP,
subnetMask: this.subnetMask,
gateway: this.gateway,
dns: this.dns,
leaseTime: "24 hours",
};
}
showLeases() {
console.log("\n=== Active DHCP Leases ===");
for (const [ip, lease] of this.leases) {
console.log(` ${ip.padEnd(16)} → ${lease.mac}`);
}
console.log("=========================");
}
}
// === Simulation ===
const server = new DHCPServer("192.168.1.0", "255.255.255.0", 100, 200);
const devices = [
"AA:BB:CC:DD:EE:01", // your laptop
"AA:BB:CC:DD:EE:02", // your phone
"AA:BB:CC:DD:EE:03", // a smart TV
];
for (const mac of devices) {
const offeredIP = server.discover(mac);
if (offeredIP) {
const config = server.request(mac, offeredIP);
console.log(` Config received:`, config);
}
}
server.showLeases();
// Output shows: each device gets a private 192.168.1.x address from the pool.
// The gateway (192.168.1.1) is the router — packets for the internet go there.
// The DNS (8.8.8.8) is Google's public resolver — used to look up domain names.
What this shows:
- DHCP automates the assignment of private IP addresses within a subnet
- Every device gets the same gateway (router) and DNS — just a different host IP
- The DHCP server tracks leases to avoid assigning the same IP twice
- This is why every device on your home network has a different
192.168.1.xaddress
Common Mistakes
Mistake 1 — Thinking 127.0.0.1 is the only loopback address
The entire 127.0.0.0/8 block — 16 million addresses — is reserved for loopback. Any address in that range loops back to the local stack. 127.0.0.1 is the convention and what localhost resolves to, but 127.0.0.2 also works. This almost never matters in practice, but the question sometimes appears in interviews.
Mistake 2 — Confusing network address with host address
The network address (e.g., 192.168.1.0) identifies the network — it cannot be assigned to any host. The broadcast address (e.g., 192.168.1.255) is reserved for sending to all hosts on that network — it also cannot be assigned. A /24 subnet has 256 addresses, but only 254 are usable for hosts. Interviewers regularly ask for the number of usable hosts in a given subnet — always subtract 2.
Mistake 3 — Thinking private IPs are globally unique
Private IP addresses (10.x.x.x, 172.16.x.x, 192.168.x.x) are reused by every network on earth. Your 192.168.1.1 is the same address as your neighbor's 192.168.1.1. They are only meaningful within their own network. NAT translates between private addresses and the one unique public IP your ISP assigns to your router. Two machines in different companies can have identical private IPs without conflict — they will never directly see each other.
Interview Questions
Q: What is the difference between a public and a private IP address?
A public IP address is globally unique and routable on the internet — it is assigned by your ISP or a cloud provider. A private IP address is from one of the three RFC 1918 ranges (10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16) and is only meaningful within a private network. Private IP addresses are never routed on the public internet — routers at the edge drop packets with private source addresses. NAT is used to allow devices with private IPs to communicate with the internet through a shared public IP.
Q: How many usable host addresses are in a /24 subnet?
A /24 subnet has 24 bits for the network and 8 bits for hosts. 2⁸ = 256 total addresses. Subtract 2 — the network address (x.x.x.0) and the broadcast address (x.x.x.255) — gives 254 usable host addresses.
Q: What is 127.0.0.1 and why is it special?
127.0.0.1 is the loopback address (commonly called localhost). A packet sent to this address never leaves the machine — it is looped back by the OS network stack before reaching any hardware. It is used to connect to services running locally on the same machine. The entire 127.0.0.0/8 block is reserved for loopback, though 127.0.0.1 is the standard.
Q: Why do home networks use 192.168.x.x?
192.168.0.0/16 is one of the three private IP address ranges defined in RFC 1918. These addresses are reserved for internal use and are never routed on the public internet. Home routers assign addresses from this range to local devices via DHCP. Because the range can be reused by any private network, millions of homes worldwide use 192.168.1.x without conflict — their devices never directly communicate with each other across the internet.
Q: What happens if a device cannot get an IP from a DHCP server?
The device self-assigns an APIPA address from the 169.254.0.0/16 range. APIPA (Automatic Private IP Addressing) is a fallback mechanism — the device picks a random address in the 169.254.x.x range and uses ARP to check that no other device on the LAN has chosen the same address. An APIPA address signals that DHCP failed. The device can communicate with other APIPA devices on the same LAN, but cannot reach the internet (no gateway, no DNS).
Quick Reference — Cheat Sheet
| Concept | Value / Range | Notes |
|---|---|---|
| IPv4 size | 32 bits | 4 octets, dotted-decimal |
| Total address space | ~4.3 billion | 2³² |
| Subnet mask purpose | Separates network/host portions | Bitwise AND with IP = network address |
| Class A | 1–126.x.x.x | /8 mask, 16M hosts/network |
| Class B | 128–191.x.x.x | /16 mask, 65K hosts/network |
| Class C | 192–223.x.x.x | /24 mask, 254 hosts/network |
| Class D | 224–239.x.x.x | Multicast |
| Class E | 240–255.x.x.x | Reserved |
| Private A | 10.0.0.0 – 10.255.255.255 | RFC 1918 |
| Private B | 172.16.0.0 – 172.31.255.255 | RFC 1918 |
| Private C | 192.168.0.0 – 192.168.255.255 | RFC 1918 |
| Loopback | 127.0.0.0/8 | 127.0.0.1 = localhost |
| APIPA | 169.254.0.0/16 | No DHCP available |
| Broadcast (subnet) | Last address in subnet | Cannot assign to host |
| Network address | First address in subnet | Cannot assign to host |
| Usable hosts in /24 | 254 | 256 − 2 (network + broadcast) |
Previous: Lesson 2.4 — Presentation & Application Layers → Next: Lesson 3.2 — IPv6 — The Future of Addressing →
This is Lesson 3.1 of the Networking Interview Prep Course — 8 chapters, 32 lessons.