Computer Networks / Practical class / Practical class 8
Practical class · Computer Networks

The CIDR (Classless Inter-Domain Routing) Concept

The CIDR topic consists of two classes. This is class 1: the basics and manual calculation. You will learn to read “/26” at a glance, convert a prefix to a mask and back, find the network, broadcast and host range with pen and paper, and then check the result with Python’s ipaddress module. In the next one, class 9 (class 2), we build on this to move on to route aggregation, VLSM and provider address allocation.

:ik-kitob: Practical class 8 ⏱ 2 academic hours (80 minutes) 🚇 Packet simulators ⚡ The number after the slash
08

Lesson goals and plan

By the end of the lesson you will…

  • explain the difference between classful (A, B, C) and classless (CIDR) addressing and why CIDR was introduced
  • convert a /n prefix to a decimal mask and a mask back to a prefix by hand
  • find the network, broadcast, first and last host for any IP/n in 1 minute using the “block size” method
  • calculate the number of addresses and hosts from a prefix (including the /31 and /32 cases)
  • choose the smallest prefix for a required number of hosts
  • automate calculations with the ipaddress module (ip_interface, ip_network)

Plan (according to the syllabus)

  1. The CIDR (Classless Inter-Domain Routing) Concept

A router knows which network my IP address belongs to without even looking at me — only at the prefix. Today, calculate on paper first, then check in Python — if both agree, you have understood!

Why was a company with 2000 computers given 65,534 addresses?

In the 1980s IP addresses were “sold” in only three sizes: class A (over 16 million addresses), class B (65,534 hosts) and class C (254 hosts). 254 is too few and 65,534 is too many — nothing in between. As a result thousands of organizations took a class B and left more than 90% of the addresses unused, while the Internet routers’ tables swelled. In 1993 a solution appeared: CIDR — setting the size of an address block to any power of two with the /n notation. The 192.168.1.15/24 notation on your phone comes from exactly this idea.

Key terms

Theory reminder

In practical class 1 you saw that an IPv4 address is 32 bits and learned classes A, B, C, and in classes 3–4 you studied subnetting. CIDR combines these ideas into one rule: “address + prefix length = network”. The class no longer matters.

Table 8.1. Classful and classless approaches
Classful (1981–1993)CIDR (1993 – today)
Network boundaryfrom the first octet: A — /8, B — /16, C — /24any /0 … /32
Block sizesonly 16,777,216, 65,536, 2562, 4, 8, …, 1024, 2048, … — any 2k
Entry172.16.0.0 (the mask is assumed to be “understood”)172.16.0.0/22 — the prefix is always written
Wastelarge: a class B with 65,534 addresses for 2000 hostssmall: a /21 (2046 hosts) for 2000 hosts
Router tableeach classful network is a separate entryadjacent blocks can be merged into one entry (class 9)
Did you know?

The central pool of free IPv4 addresses (IANA) ran out in February 2011. RIPE NCC, the regional registry for Europe and Central Asia, which Uzbekistan belongs to, allocated its last free block in November 2019. That is why saving every address — CIDR, NAT and IPv6 — matters so much today.

The CIDR (Classless Inter-Domain Routing) Concept

This is class 1: we do all the calculations by hand first. The complex cases — aggregation, VLSM, provider scenarios — are in class 9 (CIDR, class 2).

Prefix ↔ mask: memorize 8 numbers

Each octet of a mask takes only one of 9 values. If you memorize them, converting takes a second:

Table 8.2. Number of ones in an octet and the mask value
Ones012345678
Octet value0128192224240248252254255
Block size (256 − value)2561286432168421

From prefix to mask: divide n by 8. The integer part is how many octets are 255; the remainder is the number of ones in the next octet. For example /20: 20 = 8·2 + 4 → 255.255.240.0. From mask to prefix: add up the ones in each octet: 255.255.255.224 → 8+8+8+3 = /27.

Worked example 1. 192.168.10.77/26 — a full analysis

  1. Write the mask

    26 = 8·3 + 2 → 255.255.255.192. Host bits: 32 − 26 = 6.

  2. Find the interesting octet

    The octet whose mask is neither 255 nor 0 — the 4th octet (192). The calculation happens only in this octet; the earlier octets stay as they are in the address.

  3. Block size

    256 − 192 = 64. In the 4th octet the networks start at 0, 64, 128, 192.

  4. Which block is the address in?

    Round 77 down to the largest multiple of 64 that does not exceed it: 64. The network is 192.168.10.64, the next network is .128.

  5. Broadcast and hosts

    Broadcast = next network − 1 = 192.168.10.127. Hosts: .65 … .126, in total 26 − 2 = 62.

In binary (the AND operation)

IP     11000000.10101000.00001010.01|001101
Mask   11111111.11111111.11111111.11|000000
AND    11000000.10101000.00001010.01|000000
       = 192.168.10.64  (network)
Broad. 11000000.10101000.00001010.01|111111
       = 192.168.10.127

Checking in Python

import ipaddress
i = ipaddress.ip_interface("192.168.10.77/26")
i.network                    # 192.168.10.64/26
i.network.broadcast_address  # 192.168.10.127
i.network.num_addresses      # 64
i.netmask                    # 255.255.255.192

Worked example 2. The interesting octet is the 3rd: 10.45.200.9/20

  1. Mask

    20 = 8·2 + 4 → 255.255.240.0. The interesting octet is the 3rd octet.

  2. Block

    256 − 240 = 16: in the 3rd octet the networks are 0, 16, 32, …, 192, 208, …

  3. Network

    The nearest multiple of 16 below 200 is 192 (16·12). The 4th octet is always 0 → 10.45.192.0.

  4. Broadcast

    The next network is 10.45.208.0 → one before it: 10.45.207.255. Hosts 10.45.192.1 … 10.45.207.254, in total 212 − 2 = 4094.

Pitfall

10.45.193.0/20 is an ordinary host address, even though it ends in 0! It lies inside the 10.45.192.0/20 network, and the network address is only 10.45.192.0. The rule “ends in .0 — network, .255 — broadcast” works only for prefixes of /24 and longer.

Number of addresses and hosts

Table 8.3. Commonly used prefixes
PrefixMaskAddressesHostsTypical use
/16255.255.0.065 53665 534internal network of a large organization
/20255.255.240.040964094campus, provider pool
/22255.255.252.010241022large Wi-Fi network
/24255.255.255.0256254home, office, computer classroom
/26255.255.255.1926462department, floor
/29255.255.255.24886a small block of public addresses from a provider
/30255.255.255.25242link between two routers
/31255.255.255.25422point-to-point link (RFC 3021) — no network/broadcast
/32255.255.255.25511a single-host route, loopback

Formula: addresses = 232 − n, hosts = 232 − n − 2 (the network and broadcast addresses are subtracted). Exceptions: in /31 both addresses are used, /32 is a single host.

Lab: the CIDR bit visualizer

Enter an IP address and move the prefix slider. Blue bits are the network part, yellow ones are the host part. Watch how the block doubles or halves with each step of the slider, and how this differs from the classful view.

:ik-kalkulyator: CIDR bit visualizer

/26

Trainer: calculate by hand

The trainer gives you a random address. Find the network and broadcast addresses without a calculator using the block-size method. 5 correct answers in a row — a “black belt” in CIDR.

:ik-maqsad: Network and broadcast trainer

Type your answer and press “Check”.

Test runs: first predict, then check

Independent tasks

7 tasks from easy to hard. Write input() without a prompt text. Try to solve the first three without the ipaddress module — with bits: that way you will feel what happens “inside” the router. In each one, the Check button runs the tests.

Common mistakes

ErrorConsequenceCorrect answer
Writing /20 as 255.255.255.240that mask describes a network 16 times smaller (/28)20 = 8+8+4 → 255.255.240.0
Changing the octets after the interesting octet10.45.200.9/20 → a wrong network such as “10.45.192.9”in the network the following octets are 0, in the broadcast 255
Forgetting to subtract 2 from the number of hostsplanning 64 computers on a /26 — 2 of them will not fithosts = 2h − 2 (except for /31 and /32)
Thinking “ending in .0 is always the network address”rejecting valid hosts (10.45.193.0/20)calculate the block boundary
ip_network("192.168.10.77/26")ValueError: host bits setstrict=False or ip_interface
Choosing the mask by class (“10.x — so /8”)analyzing CIDR networks incorrectlyalways use the given prefix

Boss level

Boss level: the campus network “investigator”

The university network administrator gave you a list of devices: each line is name IP/n gateway. Some students wrote the addresses incorrectly by hand (recall the default gateway topic from class 7). The first line contains the number of devices k. Check each device in the order below and print the first found problem: +60 XP

  1. the IP is the network address → nom: IP tarmoq manzili
  2. the IP is the broadcast address → nom: IP broadcast manzili
  3. the gateway is not on the same network as the IP → nom: shlyuz boshqa tarmoqda
  4. the gateway is the device’s own IP → nom: shlyuz IP bilan bir xil
  5. the gateway is the network or broadcast address → nom: shlyuz yaroqsiz manzil
  6. everything is fine → nom: OK

Reinforcement

Review questions

What is CIDR and what problems did it solve?

Classless Inter-Domain Routing (1993). By setting the network boundary with any prefix, it reduced address waste, and by merging adjacent networks it slowed the growth of router tables.

What does the /n notation mean?

The first n bits of the address are the network part, the remaining 32 − n bits are the host part.

How do you convert a prefix to a mask? Use /22 as an example.

22 = 8+8+6 → 255.255.252.0 (6 ones = 252).

How is the network address found with the block-size method?

Block in the interesting octet = 256 − the mask octet; round the address octet down to the nearest multiple of the block, and set the following octets to 0.

The formula for the number of hosts and the /31 and /32 exceptions?

232−n − 2. /31 — 2 hosts (point-to-point, RFC 3021), /32 — 1 host.

Why is 10.45.193.0/20 considered a host address?

In /20 the block is 16 in the 3rd octet: the network is 10.45.192.0, the broadcast is 10.45.207.255; 10.45.193.0 lies between them.

What is the difference between ip_network and ip_interface?

ip_network is the network itself (the host bits must be zero, otherwise use strict=False); ip_interface is a specific address + prefix, from which you get .ip and .network.

What are the drawbacks of classful addressing?

Only 3 sizes — large waste; every network is a separate entry in the router table; blocks smaller or larger than a class boundary cannot be expressed.

Independent work

The independent study topic in the course syllabus: “IPv4 addressing, subnetting and efficient planning of IP addresses.” (6 hours). This class covers the manual calculation part; in class 9 you will continue with the planning part of this topic (aggregation, VLSM).

For the address in your variant: the mask, wildcard, network, broadcast, first/last host, number of hosts and the classful view — calculate by hand (with binary notation), then check with Python’s ipaddress and compare both results in a table. Extra: write what kind of organization could work with the largest number of devices that fits in this block.

Assessment criteria (individual assignment)
CriterionPoints
All parameters are calculated correctly by hand (with binary notation)2
The Python (ipaddress) check is done and the results are compared1
The difference between the classful and CIDR views is explained1
A practical conclusion on the use of blocks1

Summary

CIDRThe network boundary is set by the /n prefix, not by the class (1993).
Prefix ↔ mask128, 192, 224, 240, 248, 252, 254, 255 — memorize these 8 numbers.
Block size256 − the mask octet; round the address down to a multiple of the block.
CountsAddresses 232−n, hosts 232−n − 2; /31 and /32 are exceptions.
PitfallAn address ending in .0 can also be a host (/23, /22 …).
ipaddressip_interface → .network, .netmask, broadcast_address, num_addresses.
Next stepClass 9: aggregation, VLSM and provider allocation.

Your lesson results

Your results are saved in this browser. Read all the sections and complete the exercises and assignments.

My personal notes