# Subnetting Practice — VLSM, Route Summarisation and a Python Subnet Calculator

Source: https://skills101.in/learn/computer-networks/cn-subnetting-vlsm — Skills101 free tutorial (Computer Networks). Updated 2026-11-21.

Lesson 3 showed how to find the network and broadcast address of one subnet and how to split a network into **equal** parts. Real networks are not equal: a sales floor may have 100 laptops while a link between two routers needs only 2 addresses. **VLSM** (Variable Length Subnet Masking) gives each part a subnet of the right size. This lesson solves VLSM and summarisation questions by hand and checks them with Python.

## 1. Quick revision: the numbers you need

For a prefix **/n** there are **32 − n host bits**.

| Prefix | Block size (addresses) | Usable hosts |
|---|---|---|
| /24 | 256 | 254 |
| /25 | 128 | 126 |
| /26 | 64 | 62 |
| /27 | 32 | 30 |
| /28 | 16 | 14 |
| /29 | 8 | 6 |
| /30 | 4 | 2 |

**Smallest subnet for h hosts:** the smallest block where block − 2 ≥ h (the network and broadcast addresses cannot be given to devices). For 50 hosts: 32 − 2 = 30 is too small, 64 − 2 = 62 is enough → **/26**.

## 2. Why equal subnets waste addresses

A college gets **192.168.10.0/24** and needs: Sales 100 hosts, HR 50, IT 25, and two router-to-router links with 2 hosts each — five subnets.

With equal subnets, five subnets need 3 borrowed bits → eight **/27** subnets of 30 hosts. But Sales needs 100 — a /27 is far too small. Using /25 for everything gives only two subnets. Equal subnetting simply **cannot** fit this plan into one /24. VLSM can.

## 3. VLSM step by step

**Rule: allocate the largest need first**, each at the next free address that is a multiple of its block size. Going largest first keeps every block correctly aligned.

1. **Sales, 100 hosts** → /25 (block 128). Start at .0 → **192.168.10.0/25**, hosts .1–.126, broadcast .127.
2. **HR, 50 hosts** → /26 (block 64). Next free is .128 → **192.168.10.128/26**, hosts .129–.190, broadcast .191.
3. **IT, 25 hosts** → /27 (block 32). Next free .192 → **192.168.10.192/27**, hosts .193–.222, broadcast .223.
4. **Link-1, 2 hosts** → /30 (block 4). Next free .224 → **192.168.10.224/30**, hosts .225–.226.
5. **Link-2, 2 hosts** → /30. Next free .228 → **192.168.10.228/30**, hosts .229–.230.

Addresses .232 to .255 remain **free** for future growth.

![VLSM plan for 192.168.10.0/24: Sales 192.168.10.0/25 with 126 usable hosts, HR .128/26 with 62, IT .192/27 with 30, two /30 links at .224 and .228 with 2 each, and the rest free](https://skills101.in/learn-img/computer-networks/vlsm-plan.svg)

## 4. Check it with Python

Python's built-in `ipaddress` module knows all the subnet arithmetic, so a VLSM planner is only a few lines:

```python
import ipaddress, math

def vlsm(network, needs):
    free = ipaddress.ip_network(network)
    start = free.network_address
    plan = []
    for name, hosts in sorted(needs.items(), key=lambda kv: -kv[1]):   # biggest first
        host_bits = math.ceil(math.log2(hosts + 2))                    # + network and broadcast
        subnet = ipaddress.ip_network(f"{start}/{32 - host_bits}")
        plan.append((name, hosts, subnet))
        start = subnet.broadcast_address + 1
    return plan

needs = {"Sales": 100, "HR": 50, "IT": 25, "Link-1": 2, "Link-2": 2}
for name, hosts, net in vlsm("192.168.10.0/24", needs):
    first, last = net.network_address + 1, net.broadcast_address - 1
    print(f"{name:7} needs {hosts:3}  {str(net):18} usable {first} - {last}  ({net.num_addresses - 2} hosts)")
```

```text
Sales   needs 100  192.168.10.0/25    usable 192.168.10.1 - 192.168.10.126  (126 hosts)
HR      needs  50  192.168.10.128/26  usable 192.168.10.129 - 192.168.10.190  (62 hosts)
IT      needs  25  192.168.10.192/27  usable 192.168.10.193 - 192.168.10.222  (30 hosts)
Link-1  needs   2  192.168.10.224/30  usable 192.168.10.225 - 192.168.10.226  (2 hosts)
Link-2  needs   2  192.168.10.228/30  usable 192.168.10.229 - 192.168.10.230  (2 hosts)
```

A simpler use of the same module answers "which network is this host in?":

```python
import ipaddress

host = ipaddress.ip_interface("172.16.45.200/20")
net = host.network
print("network:  ", net.network_address)
print("broadcast:", net.broadcast_address)
print("mask:     ", net.netmask)
print("usable:   ", net.num_addresses - 2)
```

```text
network:   172.16.32.0
broadcast: 172.16.47.255
mask:      255.255.240.0
usable:    4094
```

## 5. Solving a /20 by hand

The question above, **172.16.45.200/20**, is harder than a /24 question because the "interesting" octet is the **third** one.

1. /20 → mask 255.255.**240**.0. Block size in the third octet = 256 − 240 = **16**.
2. Multiples of 16 in the third octet: 0, 16, 32, **48**… 45 falls in the **32** block (32 to 47).
3. Network = **172.16.32.0**, broadcast = **172.16.47.255**.
4. Usable hosts = 2¹² − 2 = **4094**.

The trick: find the octet where the mask is neither 255 nor 0, and do the block arithmetic **in that octet**; octets to the right are 0 for the network and 255 for the broadcast.

## 6. Route summarisation (supernetting)

Summarisation is the opposite of subnetting: combine several networks into **one shorter prefix**, so routers carry fewer routes.

*An ISP has 200.1.0.0/24, 200.1.1.0/24, 200.1.2.0/24 and 200.1.3.0/24. Give one summary route.*

Write the octet that changes in binary and find the bits common to all four:

![Four networks 200.1.0.0/24 to 200.1.3.0/24: in the third octet the first 6 bits 000000 are the same and the last 2 bits change, so 8 + 8 + 6 = 22 bits match and the summary is 200.1.0.0/22](https://skills101.in/learn-img/computer-networks/supernet.svg)

The first 8 + 8 + 6 = **22 bits** are identical, so the summary is **200.1.0.0/22**. In Python, `ipaddress.collapse_addresses()` gives the same answer.

**Conditions for a clean summary:** the number of networks is a power of 2, they are **contiguous**, and the first one starts on a boundary of the combined block (here the third octet 0 is a multiple of 4). 200.1.1.0 to 200.1.4.0 could **not** be summarised as one /22.

## 7. Typical exam questions

| Question | Answer |
|---|---|
| How many /27 subnets fit in a /24? | 2^(27 − 24) = **8** |
| Hosts in a /29? | 2³ − 2 = **6** |
| Mask for 500 hosts? | 512 block → 9 host bits → **/23**, 255.255.254.0 |
| Is 10.1.1.255/23 a usable host? | /23 network 10.1.0.0, broadcast 10.1.1.255 → **no**, it is the broadcast |
| Summary of 10.0.8.0/24 to 10.0.15.0/24? | 8 networks starting at a multiple of 8 → **10.0.8.0/21** |

The fourth row is a favourite trap: an address ending in .255 is not always a broadcast, and one ending in .0 is not always a network address — it depends on the prefix.

## Common mistakes

- Allocating small subnets first, which leaves big blocks misaligned.
- Using 2^h instead of 2^h − 2 for usable hosts.
- Doing block arithmetic in the fourth octet for a /20 (it belongs in the third).
- Summarising networks that are not contiguous or do not start on the right boundary.
- Forgetting that /31 and /32 are special cases (point-to-point links and single hosts) not covered by the − 2 rule.

## Quick recap

- VLSM: different mask lengths in one network; allocate the largest need first.
- Smallest subnet for h hosts: smallest block with block − 2 ≥ h.
- Work in the octet where the mask is neither 255 nor 0; block size = 256 − that mask octet.
- Summarisation: count the common leading bits; the networks must be contiguous and aligned.
- Python's `ipaddress` module checks any answer in seconds.


Practice quiz and free certificate: https://skills101.in/learn/computer-networks/cn-subnetting-vlsm