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.
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)
- 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!
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.
| Classful (1981–1993) | CIDR (1993 – today) | |
|---|---|---|
| Network boundary | from the first octet: A — /8, B — /16, C — /24 | any /0 … /32 |
| Block sizes | only 16,777,216, 65,536, 256 | 2, 4, 8, …, 1024, 2048, … — any 2k |
| Entry | 172.16.0.0 (the mask is assumed to be “understood”) | 172.16.0.0/22 — the prefix is always written |
| Waste | large: a class B with 65,534 addresses for 2000 hosts | small: a /21 (2046 hosts) for 2000 hosts |
| Router table | each classful network is a separate entry | adjacent blocks can be merged into one entry (class 9) |
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:
| Ones | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|---|---|---|---|---|---|---|---|---|---|
| Octet value | 0 | 128 | 192 | 224 | 240 | 248 | 252 | 254 | 255 |
| Block size (256 − value) | 256 | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
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
- Write the mask
26 = 8·3 + 2 → 255.255.255.192. Host bits: 32 − 26 = 6.
- 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.
- Block size
256 − 192 = 64. In the 4th octet the networks start at 0, 64, 128, 192.
- 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.
- 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.127Checking 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.192Worked example 2. The interesting octet is the 3rd: 10.45.200.9/20
- Mask
20 = 8·2 + 4 → 255.255.240.0. The interesting octet is the 3rd octet.
- Block
256 − 240 = 16: in the 3rd octet the networks are 0, 16, 32, …, 192, 208, …
- Network
The nearest multiple of 16 below 200 is 192 (16·12). The 4th octet is always 0 → 10.45.192.0.
- 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.
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
| Prefix | Mask | Addresses | Hosts | Typical use |
|---|---|---|---|---|
| /16 | 255.255.0.0 | 65 536 | 65 534 | internal network of a large organization |
| /20 | 255.255.240.0 | 4096 | 4094 | campus, provider pool |
| /22 | 255.255.252.0 | 1024 | 1022 | large Wi-Fi network |
| /24 | 255.255.255.0 | 256 | 254 | home, office, computer classroom |
| /26 | 255.255.255.192 | 64 | 62 | department, floor |
| /29 | 255.255.255.248 | 8 | 6 | a small block of public addresses from a provider |
| /30 | 255.255.255.252 | 4 | 2 | link between two routers |
| /31 | 255.255.255.254 | 2 | 2 | point-to-point link (RFC 3021) — no network/broadcast |
| /32 | 255.255.255.255 | 1 | 1 | a 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
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
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
| Error | Consequence | Correct answer |
|---|---|---|
| Writing /20 as 255.255.255.240 | that mask describes a network 16 times smaller (/28) | 20 = 8+8+4 → 255.255.240.0 |
| Changing the octets after the interesting octet | 10.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 hosts | planning 64 computers on a /26 — 2 of them will not fit | hosts = 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 set | strict=False or ip_interface |
| Choosing the mask by class (“10.x — so /8”) | analyzing CIDR networks incorrectly | always 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
- the IP is the network address →
nom: IP tarmoq manzili - the IP is the broadcast address →
nom: IP broadcast manzili - the gateway is not on the same network as the IP →
nom: shlyuz boshqa tarmoqda - the gateway is the device’s own IP →
nom: shlyuz IP bilan bir xil - the gateway is the network or broadcast address →
nom: shlyuz yaroqsiz manzil - 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.
| Criterion | Points |
|---|---|
| All parameters are calculated correctly by hand (with binary notation) | 2 |
| The Python (ipaddress) check is done and the results are compared | 1 |
| The difference between the classful and CIDR views is explained | 1 |
| A practical conclusion on the use of blocks | 1 |
Summary
Your lesson results
Your results are saved in this browser. Read all the sections and complete the exercises and assignments.