Skip to content
Tutorials Center
My learning

Topics

Lesson 76 of 79 ยท Practice and Review

Practice: Addressing and Routing Exercises

Practice subnetting, CIDR, VLSM, route aggregation, longest-prefix match, Dijkstra and distance-vector routing with worked solutions and live Python checks.

Reading about subnetting and routing feels easy. Doing it under time pressure, in an exam or at a router console, is a different skill, and the only way to build it is to solve problems. This lesson gives you ten exercises on addressing (subnet masks, equal and variable-length subnets, aggregation, longest-prefix match, IPv6) and routing (Dijkstra and distance-vector). Every exercise has a statement, a method and a live Python check that prints the answer.

Work like this: read the statement, solve it on paper first (the methods below are meant for paper), and only then run the code to compare. If your answer differs, the printed intermediate values usually show where you went wrong. The theory behind each exercise is in IPv4 addresses, subnetting, CIDR and VLSM, routing basics, Dijkstra and distance-vector routing.

A paper toolbox

Four short methods solve almost every addressing question.

1. Block size. In the octet where the mask is not 0 or 255, the block size is 256 - mask value. For a mask of 255.255.240.0 the interesting octet is the third, and the block size is 256 - 240 = 16. Subnets start at multiples of 16: 0, 16, 32, 48, and so on.

2. Find the network address. Take the interesting octet of the host address, round it down to a multiple of the block size, and set all later octets to 0. The broadcast address is the next network address minus 1.

3. Hosts and subnets. A prefix with h host bits gives 2^h addresses and 2^h - 2 usable hosts. Borrowing b bits from the host part gives 2^b subnets.

4. Power-of-two table. Memorize the values below; they are all you need for host counts.

Prefix Mask (last octet) Block size Usable hosts
/24 0 256 254
/25 128 128 126
/26 192 64 62
/27 224 32 30
/28 240 16 14
/29 248 8 6
/30 252 4 2

Part 1: Subnet arithmetic

Exercise 1. A host has the address 172.16.37.201/20. Find the subnet mask, the wildcard mask, the network address, the broadcast address, the first and last usable host, the number of usable hosts, and which /20 subnet of 172.16.0.0/16 this is (counting from 0).

Exercise 2. The network 192.168.10.0/24 must be divided into 6 equal subnets. Which prefix do you need? List the subnets with their host ranges, and say which subnet contains 192.168.10.100.

Method for Exercise 1: a /20 leaves 12 host bits, so the mask is 255.255.240.0 and the block size in the third octet is 16. The third octet 37 rounds down to 32, so the network is 172.16.32.0; the next network is 172.16.48.0, so the broadcast is 172.16.47.255. There are 2^12 - 2 = 4,094 usable hosts. Method for Exercise 2: six subnets need 3 borrowed bits (2^3 = 8 is the first power of two that is at least 6), so the prefix becomes /27 with block size 32.

Python Exercises 1 and 2: subnet arithmetic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line of Exercise 1 repeats the calculation the way a router does it: a bitwise AND of address and mask. Two of the eight subnets in Exercise 2 are left unused, which is normal: the number of subnets always rounds up to a power of two.

Part 2: VLSM, aggregation and longest-prefix match

Exercise 3 (VLSM). The block 10.1.0.0/22 must serve these networks: Campus (500 hosts), Labs (120), Library (60), Offices (28), and two point-to-point links (2 hosts each). Allocate the subnets, largest first, and state how much space remains.

Exercise 4 (aggregation). Can the following groups be advertised as a single route? If yes, give it; if not, give the minimum list. (a) 172.16.4.0/24 to 172.16.7.0/24. (b) 10.0.3.0/24 to 10.0.6.0/24. (c) 192.168.12.0/23 and 192.168.14.0/23.

Exercise 5 (longest-prefix match). A router has these routes: 0.0.0.0/0 via R-default, 10.0.0.0/8 via R1, 10.20.0.0/16 via R2, 10.20.30.0/24 via R3, 10.20.30.128/25 via R4, 172.16.0.0/12 via R5. Where do packets for 10.20.30.200, 10.20.30.20, 10.20.99.1, 10.99.0.1, 172.31.255.1 and 172.32.0.1 go?

Hints: In Exercise 3, sizes are 512, 128, 64, 32, 4 and 4 addresses (host count + 2, rounded up to a power of two), which is 744 of the 1,024 addresses. For aggregation, a group merges into one route only if its size is a power of two and the first network is aligned to that size: four /24 networks need a first third octet that is a multiple of 4. In Exercise 5, mentally list every matching route and pick the longest prefix. Note that 172.16.0.0/12 covers 172.16.0.0 through 172.31.255.255, so 172.32.0.1 falls outside it.

Python Exercises 3 to 5: VLSM, aggregation and routing lookup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Group (b) of Exercise 4 is the instructive one: 10.0.3.0 is odd in the third octet, so it cannot start a block of two networks, and the best you can do is 10.0.3.0/24, 10.0.4.0/23 and 10.0.6.0/24. A summary route covering all four, such as 10.0.0.0/21, would also include 10.0.0.0/24 through 10.0.2.0/24 and 10.0.7.0/24, addresses you do not own. Over-summarizing like that can attract traffic that belongs to someone else.

Part 3: Same subnet or not, and IPv6

Exercise 6. For each pair of interfaces, decide whether the two hosts can exchange packets directly on the local network or need a router: (a) 192.168.1.100/25 and 192.168.1.130/25; (b) 10.0.5.10/23 and 10.0.4.200/23; (c) 10.0.5.10/24 and 10.0.4.200/24; (d) 172.16.20.5/20 and 172.16.31.250/20.

Exercise 7. Host A is 192.168.0.10/16 and host B is 192.168.5.20/24, both on the same Ethernet segment. Who thinks the other is local? What goes wrong?

Exercise 8. Compress 2001:0db8:0000:0000:0000:ff00:0042:8329. How many /64 subnets fit in a /48, and what is the third one of 2001:db8:abcd::/48?

Method: two hosts are on the same subnet if ANDing each address with the (common) mask gives the same network address. In (a) the /25 boundary is at .128, so .100 and .130 are on opposite sides. In (b) a /23 spans two third-octet values (4 and 5), so both are in 10.0.4.0/23. IPv6 compression drops leading zeros in each group and replaces one longest run of all-zero groups with ::.

Python Exercises 6 to 8: local or remote, and IPv6

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Exercise 7 is a real-world fault, and a nasty one: pings seem to work in one direction only. A believes B is local, so it sends frames straight to B's MAC address. B receives them but believes A is remote, so it sends its replies to its default gateway. If the gateway is on the same segment, it may even forward them back, which hides the problem. Always check the mask when something works "sometimes" or in only one direction.

Part 4: Shortest paths with Dijkstra

Exercise 9. Router S sees this network (link costs on the edges):

flowchart LR
    S((S)) ---|"2"| A((A))
    S ---|"5"| B((B))
    A ---|"4"| B
    A ---|"7"| C((C))
    B ---|"1"| C
    B ---|"6"| D((D))
    C ---|"3"| D
    C ---|"8"| T((T))
    D ---|"2"| T

Nine links between seven routers, with costs in the same unit.

(a) Run Dijkstra from S by hand. Write the order in which routers are settled, the final cost to every router and S's next hop for each destination. (b) The cost of link B-C rises from 1 to 10. Which of S's routes change?

Method: keep a table of "best known cost" per router. Settle the unsettled router with the smallest cost, then try to improve each of its neighbors. Cost to a neighbor = cost of the settled router + link cost. Write the previous router with each improvement; the next hop is the second router on the path from S.

Python Exercise 9: Dijkstra before and after a change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

When B-C becomes expensive, S reaches C through A (2 + 7 = 9) instead of through B (5 + 1 = 6 before). D keeps its route through B, but the cost grows, because B-D (6) is still better than reaching D through C. Only one next hop changed, yet three costs did. A link-state protocol floods the changed link cost, and every router reruns the algorithm and finds all of this independently.

Part 5: Distance-vector and count to infinity

Exercise 10. Three routers are connected in a line: A - B - C, every link cost 1. All routers know the distance to A: B is 1 away, C is 2 away. Now the link A-B fails. Routers exchange their distance vectors once per round, all at the same time, and use 16 as "infinity". (a) What do B and C believe about A in each round? How many rounds until both agree that A is unreachable? (b) Repeat with split horizon: a router never advertises a route to the neighbor it learned it from.

flowchart LR
    A((A)) -.-|"link fails"| B((B))
    B --- C((C))

After the failure, B and C are connected only to each other, and each of them still believes that the other has a route to A.

Method: in each round, a router sets its distance to 1 plus the smallest distance advertised by a neighbor, capped at 16. Without split horizon, B accepts C's old claim "A is 2 away" (which actually goes through B). It computes 3 and tells C, which then computes 4, and the distances climb by 2 every two rounds. With split horizon, C refuses to advertise A to B because it learned about A from B, so B immediately learns that nobody else knows A.

Python Exercise 10: count to infinity and split horizon

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The plain protocol needs 15 rounds to give up and, in the meantime, both routers insist that A is reachable through each other, so packets for A bounce between them until their TTL expires. Split horizon removes the loop for this simple topology in 2 rounds. It does not solve every case (loops through three or more routers still count to infinity), which is why real protocols add more tools: a small value for infinity, hold-down timers, triggered updates and, in link-state protocols, a complete map instead of rumors. See the distance-vector lesson for the full story.

Where to go next

Repeat the exercises with different numbers: change the addresses, the prefixes, the link costs. The code prints the answer every time, so you can invent as many practice problems as you like and check them yourself. When you are comfortable, the next practice lesson covers CRC, Hamming codes, sliding windows and TCP calculations.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the network address of 172.16.37.201/20?

    A /20 has a block size of 16 in the third octet. 37 rounded down to a multiple of 16 is 32, so the network is 172.16.32.0 and the broadcast address is 172.16.47.255.
  2. 2. How many bits must you borrow to split a network into 6 equal subnets?

    2 bits give only 4 subnets, 3 bits give 8. The number of equal subnets is always a power of two, so 6 is rounded up to 8.
  3. 3. Which single route covers 10.0.4.0/24, 10.0.5.0/24, 10.0.6.0/24 and 10.0.7.0/24 exactly?

    Four /24 networks make a /22 (1,024 addresses). The first network, third octet 4, is a multiple of 4, so the block is aligned.
  4. 4. A router holds 10.0.0.0/8 and 10.20.30.0/24. Where does a packet to 10.20.30.7 go?

    Both routes contain the destination, and the router picks the most specific match.
  5. 5. Why does plain distance-vector routing count to infinity after a failure?

    Each router trusts a neighbor's route that in fact depends on itself. Split horizon, hold-down timers and a small infinity value limit the problem.

Summary

  • Subnet math is a handful of paper methods: block size = 256 minus the mask octet, round down to find the network, 2^h - 2 usable hosts, and borrowed bits give 2^b subnets.
  • VLSM allocates the largest block first, each block aligned to its own size; aggregation works only for an aligned group whose size is a power of two.
  • Routers choose the longest matching prefix; 0.0.0.0/0 matches everything and loses to any specific route.
  • Hosts compare ANDed addresses to decide between direct delivery and a gateway. A mismatched mask produces one-way connectivity.
  • Dijkstra settles routers in order of cost and records the first hop; changing one link cost can change both costs and next hops.
  • Distance-vector routing can count to infinity after a failure; split horizon and the other safeguards limit it.

Continue with link-layer and transport exercises.