Difficulty: Intermediate
What is CIDR and subnetting? Given 192.168.10.0/24, divide it into 4 equal subnets and give the range of each. Also find the network and broadcast address of 192.168.1.130/25.
Subnetting is one of those topics that feels scary until you see the trick, and then it becomes a five-line calculation you can do on a napkin. Let's first understand why it exists. Classful addressing gave you rigid sizes of 254, 65534 or 16 million hosts. CIDR, Classless Inter-Domain Routing, removes classes and lets you write any prefix length: 192.168.10.0/26 means the first 26 bits are the network, the remaining 6 bits are for hosts. Subnetting is the act of borrowing bits from the host portion to create smaller networks.
The core formulas: with n host bits, a network has 2^n addresses and 2^n minus 2 usable hosts. If you borrow b bits, you get 2^b subnets. The block size (the step between subnets) is 256 minus the interesting octet of the mask. A /26 mask is 255.255.255.192, the interesting octet is 192, so block size is 64.
Worked example one: split 192.168.10.0/24 into 4 equal subnets. We need 2^b >= 4, so b = 2, and the new prefix is /26. Mask is 255.255.255.192, block size is 64, each subnet has 64 addresses and 62 usable hosts. The subnets are 192.168.10.0/26 (hosts .1 to .62, broadcast .63), 192.168.10.64/26 (hosts .65 to .126, broadcast .127), 192.168.10.128/26 (hosts .129 to .190, broadcast .191), and 192.168.10.192/26 (hosts .193 to .254, broadcast .255). Notice each network address is a multiple of 64 and each broadcast is the next network minus one.
Worked example two: find the network and broadcast for 192.168.1.130/25. A /25 mask is 255.255.255.128, so block size is 128. The blocks in the last octet are 0 and 128. Since 130 falls in the block starting at 128, the network address is 192.168.1.128, the broadcast is 192.168.1.255, and usable hosts are .129 to .254, which is 126 hosts. The same thing with bitwise AND: the network address is the IP AND the mask, and the broadcast is the network with all host bits set to 1.
VLSM, variable length subnet masking, is the next level: different subnets get different sizes. Say you have 192.168.10.0/24 and need 100 hosts, 50 hosts and 20 hosts. Allocate the biggest first: 100 hosts needs 7 host bits so a /25 (126 hosts) at .0; 50 hosts needs 6 bits so a /26 (62 hosts) at .128; 20 hosts needs 5 bits so a /27 (30 hosts) at .192. Allocating largest first avoids fragmentation and overlap.
CIDR also enables route aggregation, or supernetting: 192.168.0.0/24 and 192.168.1.0/24 can be advertised as one route 192.168.0.0/23, which keeps internet routing tables small. When two routes match, routers use longest prefix match, so a /24 route wins over a /16 route for the same destination. Common mistakes to avoid: forgetting to subtract 2 for usable hosts, forgetting that /31 and /32 are special (point-to-point links and single hosts), and misaligning subnet boundaries, since a /26 must start at a multiple of 64.
import ipaddress
net = ipaddress.ip_network("192.168.10.0/24")
for s in net.subnets(new_prefix=26):
hosts = list(s.hosts())
print(s, hosts[0], hosts[-1], s.broadcast_address)
x = ipaddress.ip_interface("192.168.1.130/25")
print(x.network, x.network.broadcast_address)
The standard library confirms the hand calculation.
/24 255.255.255.0 256 addr 254 hosts
/25 255.255.255.128 128 addr 126 hosts
/26 255.255.255.192 64 addr 62 hosts
/27 255.255.255.224 32 addr 30 hosts
/28 255.255.255.240 16 addr 14 hosts
/29 255.255.255.248 8 addr 6 hosts
/30 255.255.255.252 4 addr 2 hosts
Memorise this; almost every subnetting problem uses one of these rows.
CIDR, Subnet Mask, VLSM, Network Address, Broadcast Address