IPv4 Subnetting Explained for Beginners
Learn IPv4 subnetting from scratch: why we subnet, how subnet masks work, how to calculate network and broadcast addresses, and common pitfalls for beginners.
Why Subnetting Exists
An IPv4 address is 32 bits. Without subnetting, every device on the internet would need a globally unique IP, and routing tables would need an entry for every single one. Subnetting groups IPs into networks so routers only need to know about networks, not individual hosts.
The original class-based system (Class A, B, C) divided the address space into rigid blocks. A Class C network always had 256 addresses whether you needed 10 or 200. CIDR replaced this with flexible prefix lengths, allowing networks of any power-of-two size. You borrow bits from the host portion to create smaller or larger subnets as needed.
The 32 Bits
An IP address is four octets, each 8 bits:
192 . 168 . 1 . 100
11000000 10101000 00000001 01100100
A subnet mask marks which bits are the network and which are the host. In a
/24 network, the first 24 bits are network, the last 8 are host:
Network bits (24): 11000000.10101000.00000001
Host bits (8): .01100100
Devices in the same network share the same network bits. They can communicate directly without a router. Devices with different network bits need a router (gateway) to forward traffic between them.
How a Subnet Mask Works
A subnet mask in binary is a run of ones followed by a run of zeros:
11111111.11111111.11111111.00000000 = 255.255.255.0 = /24.
A logical AND between the IP and the mask gives the network address:
11000000.10101000.00000001.01100100 (192.168.1.100)
& 11111111.11111111.11111111.00000000 (255.255.255.0)
───────────────────────────────────
11000000.10101000.00000001.00000000 (192.168.1.0)
The network address is 192.168.1.0. The host portion is 100 (decimal).
Any device with the same first three octets (192.168.1.x) is in the same subnet.
Subnetting in Action
You have the network 192.168.1.0/24 and need three subnets with at most 50
hosts each. A /26 subnet provides 64 total addresses (62 usable), which fits.
The subnets divide the /24 into four /26 blocks:
192.168.1.0/26 → hosts 192.168.1.1 - 192.168.1.62, broadcast .63
192.168.1.64/26 → hosts 192.168.1.65 - 192.168.1.126, broadcast .127
192.168.1.128/26 → hosts 192.168.1.129 - 192.168.1.190, broadcast .191
192.168.1.192/26 → hosts 192.168.1.193 - 192.168.1.254, broadcast .255
Each /26 borrows 2 bits from the host portion (from /24 to /26). Each
borrowed bit doubles the number of subnets and halves the number of hosts
per subnet. This is the fundamental tradeoff: more subnets means fewer hosts
per subnet.
The Formulas
For a prefix length n on a 32-bit address:
- Total addresses:
2^(32 - n) - Usable addresses:
2^(32 - n) - 2 - Number of /24 subnets you can carve into:
2^(n - 24)forn > 24 - Block size (last octet increment):
2^(32 - n)whenn >= 24
For n < 24, the block size applies to the third or second octet. A /20 has
a block size of 2^(32 - 20) = 2^12 = 4,096, which means the third octet
increments by 16 (4,096 / 256).
Real-World Example: Office Network
You need to subnet 10.0.0.0/16 for a three-floor office building:
- Floor 1: up to 200 devices (workstations, printers, phones)
- Floor 2: up to 100 devices
- Floor 3: up to 50 devices
- Server room: up to 20 devices
Start with the largest requirement. Floor 1 needs 200 hosts. A /24 provides
254 usable hosts. Assign 10.0.1.0/24.
Floor 2 needs 100 hosts. A /25 provides 126 usable hosts. Assign 10.0.2.0/25.
Floor 3 needs 50 hosts. A /26 provides 62 usable hosts. Assign 10.0.3.0/26.
Server room needs 20 hosts. A /27 provides 30 usable hosts. Assign
10.0.4.0/27.
Each subnet is sized to its requirement with some room for growth. None overlap.
The router at 10.0.0.1 connects all four subnets.
VLSM (Variable Length Subnet Masking)
The example above uses VLSM: different prefix lengths within the same network.
Without VLSM, you would give every subnet the same size (say /24), wasting
hundreds of IPs on small subnets. VLSM lets you right-size each subnet.
The catch: subnets must not overlap. If you assign 10.0.2.0/25 (hosts .1 to
.126) and then assign 10.0.2.64/26 (hosts .65 to .126), those two subnets
overlap . addresses .65 through .126 belong to both. The router can't determine
which subnet a packet belongs to.
Use the IP/CIDR Calculator to verify subnets don't overlap before deploying.
Common Gotchas
- Network and broadcast addresses are not usable for hosts. A
/30has 4 total addresses but only 2 usable. Point-to-point links use/30or/31(RFC 3021). Forgetting this means your DHCP scope runs out attotal - 2, not at the total. - The first and last subnets in a range were historically avoided ("subnet zero" rule). This is obsolete. Modern equipment and operating systems support subnet zero without any special configuration.
- Overlapping subnets: two subnets that share any IP range produce routing conflicts. Packets may go to the wrong subnet or be dropped entirely.
- Gateway address placement: the gateway is typically the first usable
address (
.1) or the last (.254). Be consistent. A device with the wrong gateway IP can reach its local subnet but nothing beyond it. - Confusing prefix length with subnet mask:
/24is a prefix length.255.255.255.0is the same thing in mask notation. Learn to convert between them in your head. It speeds up debugging.
Try it yourself: open the IP/CIDR Calculator. Enter
192.168.1.0/26and confirm the subnet mask is255.255.255.192, the host range is.1through.62, and the broadcast is.63. Then try/25and observe how the host count doubles to 126. Enter10.0.0.0/20and see how the subnet mask spans the third octet.