G27
Self-checkedTier 1 7 min readcn.cidr-subnetting

CIDR and subnetting

Classless addressing with the a.b.c.d/n notation: the mask from the prefix length, the network and broadcast address, usable hosts 2^(32-n) - 2, splitting a block into equal subnets by borrowing host bits, variable-length subnet masks (VLSM) allocation largest first, route aggregation (supernetting) and when blocks can be merged, and forwarding by longest prefix match; every calculation was cross-checked with Python's ipaddress module on thousands of random cases.

Why it exists

Classful addresses wasted space and bloated routing tables. CIDR (classless inter-domain routing) lets the boundary between the network part and the host part fall at any bit: an address block is written a.b.c.d/n where nn is the prefix length, the number of leading bits that identify the network. Blocks can be cut into smaller ones (subnetting), several adjacent blocks can be advertised as one (aggregation), and routers forward by the longest matching prefix. GATE asks for the arithmetic in all of these.

Prerequisites

You need the structure of an IPv4 address (IPv4 addressing).

Precise definitions

A CIDR block a.b.c.d/n is the set of the 232−n2^{32-n} addresses that share the first nn bits. The mask is nn ones followed by 32−n32 - n zeros (prefix /20/20: 255.255.240.0). For an address AA in the block:

  • network address =AANDmask= A \mathbin{\text{AND}} \text{mask} (host bits cleared);
  • broadcast address =AOR¬mask= A \mathbin{\text{OR}} \lnot\text{mask} (host bits set);
  • usable host addresses =232−n−2= 2^{32-n} - 2 (network and broadcast are not given to hosts); first host == network +1+ 1, last host == broadcast −1- 1.

A block is aligned: its first address is a multiple of its size 232−n2^{32-n}. Any block of a given size starts at such a multiple.

Subnetting divides a block (a network) into smaller blocks by moving the boundary to the right: borrowing bb host bits gives 2b2^b subnets, each with prefix n+bn + b.

VLSM (variable length subnet masks) uses subnets of different sizes so that each department gets about what it needs.

Aggregation (supernetting, route summarisation) replaces several prefixes by one that covers exactly the same addresses.

Longest prefix match. A router's forwarding table has entries prefix/length → next hop. A destination address may match several entries; the router chooses the matching entry with the longest prefix (the most specific route). The default route 0.0.0.0/0 matches everything and has the lowest priority.

How it works

Computing the block of an address

Network, broadcast and hosts of 172.16.37.5/20: start

Computed by a script, not typed
quantitydotted decimalbinary
address
mask
network address (address AND mask)
broadcast (network OR NOT mask)
first host
last host
number of hosts

Step 0

Address 172.16.37.5/20.

0 / 7
computed by script: exact integer and fraction arithmetic, address calculations on bit patterns. python3 scripts/gen-cn2-traces.py

For 172.16.37.5/20: the mask has 2020 ones: 255.255.240.0. ANDing with the third byte 37=0010010137 = 00100101 and 240=11110000240 = 11110000 gives 00100000=3200100000 = 32, so the network is 172.16.32.0. Setting the 1212 host bits to one gives the broadcast 172.16.47.255. There are 212=40962^{12} = 4096 addresses and 40944094 hosts.

Network, broadcast and hosts of 192.168.10.77/26: start

Computed by a script, not typed
quantitydotted decimalbinary
address
mask
network address (address AND mask)
broadcast (network OR NOT mask)
first host
last host
number of hosts

Step 0

Address 192.168.10.77/26.

0 / 7
computed by script: exact integer and fraction arithmetic, address calculations on bit patterns. python3 scripts/gen-cn2-traces.py

For 192.168.10.77/26 the block size is 6464; the interesting octet is the last: the networks start at 0,64,128,1920, 64, 128, 192 and 7777 lies in the block starting at 6464: network 192.168.10.64, broadcast 192.168.10.127, 6262 hosts.

Shortcut for the interesting octet: block size =256−= 256 - (mask octet); the network address is the largest multiple of the block size not exceeding the address octet.

Splitting a block into subnets

Borrowing bb bits from a /24 gives:

Splitting a /24 block: prefix length, subnets, hosts: start

Computed by a script, not typed
prefixmasksubnets of the /24addresses per subnetusable hosts per subnet
/24
/25
/26
/27
/28
/29
/30

Step 0

Borrowing b bits from the host part gives 2^b subnets; each has 2^(32 - n) addresses.

0 / 7
computed by script: exact integer and fraction arithmetic, address calculations on bit patterns. python3 scripts/gen-cn2-traces.py

To cut 192.168.10.0/24 into 44 equal subnets borrow 22 bits:

192.168.10.0/24 cut into 4 equal subnets: start

Computed by a script, not typed
subnetnetworkusable hostsbroadcast
subnet 1
subnet 2
subnet 3
subnet 4

Step 0

Borrowing 2 host bits gives 4 subnets of 64 addresses (62 usable).

0 / 4
computed by script: exact integer and fraction arithmetic, address calculations on bit patterns. python3 scripts/gen-cn2-traces.py

Each subnet has 6464 addresses (6262 usable). To serve hh hosts the prefix must satisfy 232−n−2≥h2^{32-n} - 2 \ge h; the largest suitable prefix is n=32−⌈log⁡2(h+2)⌉n = 32 - \lceil \log_2 (h + 2) \rceil.

VLSM

An office network 192.168.1.0/24 needs subnets for 100100, 5050, 2525 and 1010 hosts. Allocate the largest first, each block aligned to its own size:

VLSM: subnets for 100, 50, 25 and 10 hosts inside 192.168.1.0/24: start

Computed by a script, not typed
requirementprefixaddressessubnetusable hosts
100 hosts
50 hosts
25 hosts
10 hosts

Step 0

Variable length subnet masks: allocate the largest request first so that every block stays aligned. The blocks use 128 + 64 + 32 + 16 = 240 of the 256 addresses.

0 / 4
computed by script: exact integer and fraction arithmetic, address calculations on bit patterns. python3 scripts/gen-cn2-traces.py

100100 hosts need a /25 (126126 usable), 5050 a /26 (6262), 2525 a /27 (3030), 1010 a /28 (1414). They occupy 128+64+32+16=240128 + 64 + 32 + 16 = 240 of 256256 addresses, leaving 192.168.1.240/28 free. Allocating the small ones first would break alignment and may not fit.

Aggregation

Route aggregation (supernetting): start

Computed by a script, not typed
caseprefixesafter aggregationroutes
four contiguous /24 blocks
two blocks that are adjacent but not aligned
a block and its more specific
two /25 halves

Step 0

Aggregation replaces several prefixes by one that covers exactly the same addresses.

0 / 4
computed by script: exact integer and fraction arithmetic, address calculations on bit patterns. python3 scripts/gen-cn2-traces.py

Four consecutive /24 blocks 200.1.0.0 to 200.1.3.0 combine into 200.1.0.0/22, because the first block starts at a multiple of 4×256=10244 \times 256 = 1024 (third octet 00, a multiple of 44) and they are adjacent. The blocks 200.1.1.0/24 and 200.1.2.0/24 are adjacent but cannot be merged into one prefix: the first is not aligned to 512512. A block already covered by a shorter prefix (10.1.0.0/16 inside 10.0.0.0/8) can be dropped from the advertisement when it has the same next hop.

Longest prefix match

Longest prefix match in a forwarding table: start

Computed by a script, not typed
destinationmatching entrieschosen entry → next hop
10.1.2.200
10.1.2.5
10.1.9.9
10.200.0.1
192.168.0.1

Step 0

Forwarding table: 0.0.0.0/0 → R0 (default), 10.0.0.0/8 → R1, 10.1.0.0/16 → R2, 10.1.2.0/24 → R3, 10.1.2.128/25 → R4. A router picks, among all matching prefixes, the one with the most bits.

0 / 5
computed by script: exact integer and fraction arithmetic, address calculations on bit patterns. python3 scripts/gen-cn2-traces.py

The table has the default route and four nested prefixes. The destination 10.1.2.200 matches all five; the /25 is the most specific, so it is used. 10.1.2.5 is not in 10.1.2.128/25 and falls to the /24. 192.168.0.1 matches only the default route.

Worked examples

EX 1Hosts in a block

Easy

200.10.20.0/22 has 210=10242^{10} = 1024 addresses, so 10221022 usable hosts. A /28 has 1414, a /30 has 22 (used on point-to-point links between routers), a /24 has 254254.

EX 2Network address

Medium

130.50.77.200/18: the mask is 255.255.192.0; the third octet 77=0100110177 = 01001101 AND 192=11000000192 = 11000000 gives 01000000=6401000000 = 64. Network 130.50.64.0, broadcast 130.50.127.255 (the block size in the third octet is 6464, so the third octet runs from 6464 to 127127).

EX 3Number of subnets

Medium

A /24 split into subnets of /27: 227−24=82^{27-24} = 8 subnets, each with 3232 addresses and 3030 hosts. The fourth octets of the networks are 0,32,64,96,128,160,192,2240, 32, 64, 96, 128, 160, 192, 224.

EX 4Typical questions

GATE level

(a) "Which mask gives at least 500500 hosts per subnet?" 29−2=510≥5002^9 - 2 = 510 \ge 500, so 99 host bits, /23, 255.255.254.0. (b) "Smallest block for 22 router interfaces plus network and broadcast?" /30. (c) "Are 10.1.4.0/24 and 10.1.5.0/24 summarised by 10.1.4.0/23?" Yes: aligned to 512512 (third octet 44 is even) and adjacent. (d) "Is 10.1.5.0/24 and 10.1.6.0/24 summarised by a /23?" No: the first block's third octet 55 is odd. (e) "The router has routes /16 and /24 that match; which is used?" The /24.

Cheat box

Cheat box

  • Block a.b.c.d/n: 232−n2^{32-n} addresses, 232−n−22^{32-n} - 2 hosts; mask nn ones; network == AND, broadcast == OR with the inverted mask.
  • Block size in the interesting octet =256−mask octet= 256 - \text{mask octet}; networks at multiples of it.
  • Borrow bb bits: 2b2^b subnets of prefix n+bn + b.
  • VLSM: largest request first, aligned.
  • Aggregate only adjacent blocks of equal size where the first is aligned to twice the size.
  • Forwarding: longest prefix match; default route 0.0.0.0/0.

Traps and GATE patterns

Connections

CIDR keeps routing tables short by aggregating prefixes (distance vector routing, link state routing). Private blocks and NAT save addresses (NAT). The IP header and fragmentation are on the next pages (fragmentation). Longest prefix match is a search in a binary trie of address bits, a tree like the ones in binary trees.

PYQs

14 previous year questions from your book. I re-solved each one myself (14 by running or simulating, 0 by reasoning only); the book's key is shown next to my answer. All PYQs

GATE 2026 · NATQ1 · page 547 of the book
If an IP network uses a subnet mask of
255.255.240.0, the maximum number of IP
addresses that can be assigned to network interfaces
is ______. (answer in integer)
GATE 2026 · MSQ · 2 marksQ2 · page 547 of the book
An ISP having an address block 202.16.0.0/15
assigns a block of 6000 IP addresses to a client
using the Classless Internet Domain Routing
(CIDR) supernetting approach.

Which of the following address blocks can be
assigned by the ISP?
  • (a)202.16.0.0/19
  • (b)202.17.64.0/19
  • (c)202.16.32.0/19
  • (d)202.17.24.0/19
GATE 2025 · MCQQ10 · page 548 of the book
A
packet
with
the
destination
IP
address
145.36.109.70 arrives at a router whose routing table
is shown. Which interface will the packet be
forwarded to?

Subnet
Address
Subnet Mask
(in
CIDR
notation)
Interface
145.36.0.0
/16
E1
145.36.128.0
/17
E2
145.36.64.0
/18
E3
145.36.255.0
/24
E4
Default
--
E5
  • (a)E3
  • (b)E1
  • (c)E2
  • (d)E5
GATE 2024 · MCQQ15 · page 549 of the book
Which one of the following CIDR prefixes exactly
represents the range of IP address 10.12.2.0 to
10.12.3.255?
  • (a)10.12.2.0/24
  • (b)10.12.2.0/22
  • (c)10.12.0.0/22
  • (d)10.12.2.0/23
GATE 2024 · NATQ17 · page 549 of the book
Consider the entries shown below in the forwarding
table of an IP router. Each entry consists of an IP
prefix and the corresponding next hop router for
packets whose destination IP address matches the
prefix. The notation "/N" in a prefix indicates a
subnet mask with the most significant N bits set to 1.

Prefix
Next hop router
10.1.1.0/24
R1
10.1.1.128/25 R2
10.1.1.64/26
R3
10.1.1.192/26 R4

This router forwards 20 packets each to 5 hosts. The
IP addresses of the hosts are 10.1.1.16, 10.1.1.72,
10.1.1.132,
10.1.1.191,
and
10.1.1.205.
The number of packets forwarded via the next hop
router R2 is ________.

COMPUTER NETWORK
GATE 2023 · NAT · 2 marksQ25 · page 551 of the book
The forwarding table of a router is shown below:

Subnet
Number
Subnet mask
Interface
ID
200.150.0.0
255.250.0.0
1
200.150.64.0
255.255.224.0
2
200.150.68.0
255.255.255.0
3
200.150.68.64 255.255.255.224 4
Default

0

A packet addressed to a destination address
200.150.68.118

arrives at the router. It will be forwarded to the
interface

with ID ________.
GATE 2022 · MSQ · 2 marksQ28 · page 551 of the book
Consider the routing table of an organization's router
shown below:

Subnet number
Subnet mask
Next hop
12.20.164.0
255.255.252.0
R1
12.20.170.0
255.255.254.0
R2
12.20.168.0
255.255.254.0
Interface 0
12.20.166.0
255.255.254.0
Interface 1
default

R3

Which of the following prefixes in CIDR notation can
be collectively used to correctly aggregate all of the
subnets in the routing table?
  • (a)12.20.164.0/20
  • (b)12.20.164.0/22
  • (c)12.20.164.0/21
  • (d)12.20.168.0/22
GATE 2020 · MCQ · 2 marksQ38 · page 554 of the book
An organization requires a range of IP addresses to
assign one to each of its 1500 computers. The
organization has approached an Internet Service
Provider (ISP) for this task. The ISP uses CIDR and
serves the requests from the available IP address
space 202.61.0.0/17. The ISP wants to assign an
address space to the organization which will
minimize the number of routing entries in the ISP's
router using route aggregation. Which of the
following address spaces are potential candidates
from which the ISP can allot any one to the
organization?

I. 202.61.84.0/21
II. 202.61.104.0/21
III. 202.61.64.0/21
IV. 202.61.144.0/21
  • (a)I and IV only
  • (b)III and IV only
  • (c)I and II only
  • (d)II and III only
GATE 2015 · MCQQ60 · page 557 of the book
Consider the following routing table at an IP
router:
Network No
Net Mask
Next Hop
128.96 .170 .0
255.255 .254 .0
Interface 0
128.96 .168 .0
255.255 .254 .0
Interface 1
128.96 .166 .0
255.255 .254 .0
R2
128.96 .164 .0
255.255 .252 .0
R3
0.0 .0 .0
Default
R4

For each IP address in Group I Identify the
correct choice of the next hop from Group II
using the entries from the routing table above.

Group I
Group II
i) 128.96 .171 .92
  • (a)Interface 0 ii) 128.96 .167 .151
  • (b)Interface 1 iii) 128.96 .163 .151
  • (c)R2 iv) 128.96 .164 .121
  • (d)R3 e) R4 A. i-a, ii-c, iii-e, iv-d B. i-a, ii-d, iii-b, iv-e c, j-b, ii-c, iii-d, iv-e D. j-b, ij-c, iii-e. iv-d
GATE 2014 · NATQ68 · page 558 of the book
An IP router implementing Classless Interdomain
Routing (CIDR) receives a packet with address
131.23.151.76. The router's routing table has the
following entries:

Prefix
Output Interface Identifier
131.16.0/12
3
131.28.0.0/4
5
131.19.0.0/16
2
131.22.0.0/15
1

The identifier of the output interface on which this
packet will be forwarded is ______.

COMPUTER NETWORK
GATE 2012 · MCQ · 2 marksQ73 · page 559 of the book
An Internet Service Provider (ISP) has the following
chunk of CIDR-based IP addresses available with it:
245.248.128.0/20. The ISP wants to give half of this
chunk of addresses to Organization A, and a quarter
to Organization B, while retaining the remaining with
itself. Which of the following is a valid allocation of
addresses to A and B?
  • (a)245.248.136.0/21 and 245.248.128.0/22
  • (b)245.248.128.0/21 and 245.248.128.0/22
  • (c)245.248.136.0/22 and 245.248.132.0/21
  • (d)245.248.136.0/24 and 245.248.132.0/21
GATE 2008 · MCQ · 2 marksQ83 · page 561 of the book
If a class B network on the Internet has a subnet mask
of 255.255.248.0, what is the maximum number of
hosts per subnet?
  • (a)1022
  • (b)1023
  • (c)2046
  • (d)2047
GATE 2007 · MCQ · 2 marksQ87 · page 562 of the book
The address of a class B host is to be split into
subnets with a 6-bit subnet number. What is the
maximum number of subnets and the maximum
number of hosts in each subnet?
  • (a)62 subnets and 262142 hosts
  • (b)64 subnets and 262142 hosts
  • (c)62 subnets and 1022 hosts
  • (d)64 subnets and 1024 hosts
GATE 2005 · MCQ · 1 markQ91 · page 562 of the book
An organization has a class B network and wishes to
form subnets for 64 departments. The subnet mask
would be:
  • (a)255.255.0.0
  • (b)255.255.64.0
  • (c)255.255.128.0
  • (d)255.255.252.0 COMPUTER NETWORK

Self-check

Question 1

Network address and number of hosts of 100.20.30.40/21?

Show answer

Third octet 30=0001111030 = 00011110, mask octet 248=11111000248 = 11111000: AND gives 2424. Network 100.20.24.0, broadcast 100.20.31.255, 211−2=20462^{11} - 2 = 2046 hosts.

Question 2

How many /30 subnets fit in a /24?

Show answer

26=642^6 = 64, each with 22 usable addresses.

Question 3

Aggregate 172.16.8.0/24, 172.16.9.0/24, 172.16.10.0/24, 172.16.11.0/24.

Show answer

172.16.8.0/22 (third octets 88 to 1111; 88 is a multiple of 44).

Revision card

One-screen revision

  • /n: mask nn ones, 232−n2^{32-n} addresses, 232−n−22^{32-n} - 2 hosts.
  • Network == address AND mask; broadcast sets host bits.
  • Subnet: borrow bb bits →\to 2b2^b subnets. VLSM: largest first.
  • Aggregate aligned adjacent blocks; forward by longest prefix.

References

  • ·Kurose, Ross, Computer Networking: A Top-Down Approach (8th edition), Chapter 4, Section 4.3.3 (Classless interdomain routing)
  • ·Tanenbaum, Wetherall, Computer Networks (5th edition), Chapter 5, Section 5.6.2 (Subnets, CIDR, classful and special addressing)
  • ·Forouzan, Data Communications and Networking (5th edition), Chapter 19, Section 19.2 (Classless addressing)

Verification

scripts/enum-checks/cn_net_check.py against Python's ipaddress module: network, broadcast, mask and host counts on 5000 random address/prefix pairs; splitting a block into 2^k subnets equals ip_network.subnets on 500 random blocks; VLSM allocations on 1000 random request lists are aligned, disjoint, inside the block, large enough and the smallest block that suffices (largest-first allocation never failed when the block sizes summed to at most the block); aggregation equals ipaddress.collapse_addresses on 1500 random sets of prefixes (the rule: merge two blocks of equal size that are adjacent and whose first block is aligned to twice the size; drop blocks covered by another); longest prefix match equals a brute-force scan on 1500 random forwarding tables. CONVENTIONS: usable hosts 2^(32-n) - 2 (the textbook rule; /31 point-to-point links (RFC 3021) and /32 host routes are exceptions mentioned but not computed); subnets with all-zeros and all-ones subnet bits are allowed (modern CIDR); older books exclude them, which changes 'number of subnets' answers by 2: exam questions state which they assume. The remark that longest prefix match is a search in a binary trie is an analogy, not a claim about router hardware. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (CRC remainders, binomial tail, checksums); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose). No defect found. Status raised to self-checked; verified needs a textbook or human check.

last verified: never