FirstHack Learn
Log in Sign up free
Lessons in this course 0/6 All courses Computer Networks

CSE

Progress0 / 6 lessons
  1. 1. What happens when you type a URL and press enter
  2. 2. The layered model, without reciting it like a poem
  3. 3. IP addressing and subnetting, with worked examples
  4. 4. TCP vs UDP, and why each one exists
  5. 5. DNS, DHCP and ARP: the three protocols that make it work
  6. 6. HTTP, HTTPS, and what TLS actually protects

Courses › Computer Networks

IP addressing and subnetting, with worked examples

Masks, prefixes and block size, then two full sums solved digit by digit.

13 min read · Lesson 3 of 6 · Free

An IP address is 32 bits, written for humans

192.168.10.37 is four bytes shown in decimal with dots. In binary:

11000000.10101000.00001010.00100101

Each octet is 0 to 255. That gives 2^32 addresses, about 4.3 billion, which the world ran out of years ago. IPv6 uses 128 bits and will not run out.

Every address splits into two parts: a network part and a host part. The split is defined by the subnet mask, which is simply a run of 1 bits followed by a run of 0 bits.

255.255.255.0 is 11111111.11111111.11111111.00000000 — 24 ones. So we write it as /24. The number after the slash is just how many 1 bits the mask has.

The four numbers you need for any subnet

For any address and prefix, you can compute:

  • Network address — host bits all 0. Names the subnet. Not usable by a device.
  • Broadcast address — host bits all 1. Reaches everyone on the subnet. Not usable by a device.
  • First and last usable host — network + 1, and broadcast - 1.
  • Usable hosts = 2^(host bits) - 2, subtracting the two above.

The fastest method uses block size. Block size = 256 minus the mask value in the octet that is not 0 or 255. Subnets then start at multiples of the block size.

Worked example 1: split 192.168.10.0/24 into four subnets

A /24 has 8 host bits. To get four subnets you need 2 extra network bits, because 2^2 = 4. So the new prefix is /26.

/26 mask: 26 ones = 11111111.11111111.11111111.11000000 = 255.255.255.192

Block size = 256 - 192 = 64. So subnets begin at 0, 64, 128, 192.

Host bits left = 32 - 26 = 6, so usable hosts = 2^6 - 2 = 62 per subnet.

Subnet Network First host Last host Broadcast
1 192.168.10.0 192.168.10.1 192.168.10.62 192.168.10.63
2 192.168.10.64 192.168.10.65 192.168.10.126 192.168.10.127
3 192.168.10.128 192.168.10.129 192.168.10.190 192.168.10.191
4 192.168.10.192 192.168.10.193 192.168.10.254 192.168.10.255

Check the arithmetic: subnet 2 starts at 64 and the next starts at 128, so its broadcast is 128 - 1 = 127, and its last host is 126. Every row follows that pattern.

You can verify all of it in Python:

Python 3
import ipaddress

net = ipaddress.ip_network("192.168.10.0/24")
for sub in net.subnets(new_prefix=26):
    hosts = list(sub.hosts())
    print(sub, "| first", hosts[0], "| last", hosts[-1],
          "| broadcast", sub.broadcast_address, "|", len(hosts), "hosts")

Worked example 2: given 172.16.45.200/20, find everything

This is the exam favourite, because the boundary is not on an octet edge.

/20 mask: 20 ones = 11111111.11111111.11110000.00000000 = 255.255.240.0

The interesting octet is the third, value 240. Block size = 256 - 240 = 16. So third-octet subnets start at 0, 16, 32, 48, 64, and so on.

Our address has 45 in the third octet. Which block contains 45? 45 divided by 16 is 2 remainder 13, so it is in block 2, which starts at 32. The next block starts at 48.

  • Network address = 172.16.32.0
  • Broadcast = one below the next network start = 172.16.48.0 minus one = 172.16.47.255
  • First host = 172.16.32.1
  • Last host = 172.16.47.254
  • Host bits = 32 - 20 = 12, so usable hosts = 2^12 - 2 = 4094
💡

Do not convert to binary under exam pressure. Compute the block size, divide the relevant octet by it, take the floor, multiply back. Three operations, no binary, no mistakes. It works for every prefix.

Private addresses and NAT

Three ranges are reserved for private networks and are never routed on the public internet:

  • 10.0.0.0/8 — 10.0.0.0 to 10.255.255.255
  • 172.16.0.0/12 — 172.16.0.0 to 172.31.255.255
  • 192.168.0.0/16 — 192.168.0.0 to 192.168.255.255

Your hostel Wi-Fi almost certainly hands you one of these. Your router then does NAT, replacing your private source address with its single public address on the way out and reversing the swap on the way back, using the port number to tell your traffic apart from your roommate's. That is how one public IP serves fifty devices, and it is the main reason IPv4 survived past its predicted exhaustion.

⚠️

172.16.0.0/12 covers 172.16 through 172.31 only. 172.32.5.1 is a public address, not a private one. Students routinely assume all of 172.x is private and lose marks. The /12 prefix means the first 12 bits are fixed: 172 plus the top 4 bits of the second octet.

Special addresses worth knowing

  • 127.0.0.1 — loopback. Traffic never leaves the machine. The whole 127.0.0.0/8 block is loopback.
  • 0.0.0.0 — "this host" or, in a routing table, "any destination", which makes it the default route.
  • 255.255.255.255 — limited broadcast, used by DHCP before a client has any address.
  • 169.254.0.0/16 — link-local. If you see one of these on your machine, DHCP failed. It is a genuinely useful diagnostic.

A point-to-point link between two routers needs exactly two usable addresses. A /30 gives 2^2 - 2 = 2 usable hosts, so it is the classic choice and wastes two addresses per link. A /31 is a special case allowed by RFC 3021 for point-to-point links, where both addresses are usable and no broadcast exists. Mentioning that in an interview shows you understand why the minus two exists rather than just applying it.