Computer Networks
IP Addressing & Subnetting
Subnetting is the most repeated Computer Networks numerical. Learn to read an address with a mask, find the network, broadcast and usable range, allocate VLSM blocks, aggregate CIDR prefixes and pick the longest matching route.
GATE CSE IPv4 addressing — classful addressing, network and broadcast addresses, subnet masks, subnetting, VLSM, CIDR aggregation, longest-prefix matching, private addresses, NAT, ARP, DHCP and IPv6 basics, with samples, flashcards and drills.
Learning outcomes
- Identify address class, default mask, network and host portions
- Compute network address, broadcast address and usable hosts for any prefix length
- Design equal-size and variable-size (VLSM) subnets
- Aggregate prefixes with CIDR and apply longest-prefix matching
IPv4 addresses and classful addressing
- An IPv4 address is a 32-bit number written as four decimal bytes (dotted decimal). It has a NETWORK part (net-id) and a HOST part (host-id). An address identifies an interface, not a machine.
- CLASSFUL addressing: Class A starts with 0 (1.0.0.0 to 126.255.255.255, default mask /8, 2^24 - 2 hosts); Class B starts with 10 (128 to 191, /16, 2^16 - 2 hosts); Class C starts with 110 (192 to 223, /24, 254 hosts); Class D starts with 1110 (224 to 239, MULTICAST); Class E starts with 1111 (240 to 255, reserved).
- SPECIAL addresses: host-id all 0s = the NETWORK address; host-id all 1s = the DIRECTED BROADCAST address; 255.255.255.255 = LIMITED broadcast (this network only); 127.x.x.x = LOOPBACK; 0.0.0.0 = this host / default route; 169.254.0.0/16 = link-local (APIPA).
- PRIVATE (non-routable on the Internet) ranges: 10.0.0.0/8, 172.16.0.0 to 172.31.255.255 (172.16.0.0/12) and 192.168.0.0/16.
- USABLE hosts in a network of h host bits = 2^h - 2 (the network and broadcast addresses cannot be assigned to hosts).
- SUPPORT protocols: ARP maps an IP address to a MAC address on the same LAN (broadcast request, unicast reply); RARP/DHCP assigns addresses (DHCP uses UDP ports 67/68 with DORA: Discover, Offer, Request, Ack); NAT lets many private hosts share one public address by rewriting addresses and port numbers; ICMP reports errors.
- NUMBER OF NETWORKS: Class A has 2^7 = 128 first-byte values but 0 and 127 are reserved, giving 126 usable networks; Class B has 2^14 = 16,384 networks; Class C has 2^21 = 2,097,152 networks. Identify the class from the leading bits: 0xxxxxxx = A, 10xxxxxx = B, 110xxxxx = C, 1110xxxx = D.
- Why classful addressing failed: only three network sizes exist, so an organisation with 500 hosts gets a Class B (65,534 addresses, over 99% unused) or two Class C blocks. This waste and the explosion of routing-table entries led to SUBNETTING, then CIDR (variable-length prefixes) and NAT.
- ADDRESS VALIDITY checks for a host: the host part must not be all 0s (network address) or all 1s (broadcast), and the address must not be 127.x.x.x or a multicast/reserved address. Private ranges are exactly 10.0.0.0 to 10.255.255.255, 172.16.0.0 to 172.31.255.255 and 192.168.0.0 to 192.168.255.255; 172.32.0.5 is therefore public.
| Class | First bits | Range of first byte | Default mask | Hosts per network |
|---|---|---|---|---|
| A | 0 | 1 - 126 | 255.0.0.0 (/8) | 16,777,214 |
| B | 10 | 128 - 191 | 255.255.0.0 (/16) | 65,534 |
| C | 110 | 192 - 223 | 255.255.255.0 (/24) | 254 |
| D | 1110 | 224 - 239 | multicast, no host part | - |
| E | 1111 | 240 - 255 | reserved | - |
Worked example
Problem. Identify the class, network address and directed broadcast address of 150.10.5.1.
Solution. First byte 150 is between 128 and 191, so Class B with default mask 255.255.0.0. Network address = 150.10.0.0; directed broadcast = 150.10.255.255. Validity checks: 200.1.2.255 (Class C, host part all 1s) is a broadcast address, not a host; 10.0.0.0 is a network address; 127.0.0.1 is loopback; 172.32.0.5 is public because the private block ends at 172.31.255.255; 224.0.0.5 is multicast (Class D).
Subnetting, VLSM and the usable range
- A SUBNET MASK has 1s for the network + subnet bits and 0s for the host bits. NETWORK ADDRESS = IP AND mask. BROADCAST ADDRESS = network address with all host bits set to 1. FIRST usable = network + 1, LAST usable = broadcast - 1.
- Prefix length /n means n mask bits are 1: addresses per block = 2^(32 - n), usable hosts = 2^(32 - n) - 2. Borrowing b bits from the host part gives 2^b subnets (classless subnetting also allows the all-0s and all-1s subnet).
- Quick method: find the interesting octet, compute BLOCK SIZE = 256 - (mask value in that octet), then the network is the largest multiple of the block size not exceeding the address in that octet.
- Two hosts are in the SAME subnet if (IP1 AND mask) = (IP2 AND mask). A host sends directly to a destination in its own subnet and sends to the DEFAULT GATEWAY otherwise.
- VLSM (variable-length subnet masks): allocate the LARGEST requirement first, giving each subnet the smallest power of two (plus 2) that fits its host count; aligned blocks must start at a multiple of their own size.
- EQUAL-SIZE RECIPE: to get at least S subnets from a /n block borrow b = ceil(log2 S) bits, so the new prefix is n + b and each subnet has 2^(32 - n - b) - 2 hosts. To give each subnet at least H hosts keep h = ceil(log2(H + 2)) host bits, so the prefix is 32 - h.
- A valid mask is a run of 1s followed by 0s. The values of the interesting octet are 128, 192, 224, 240, 248, 252, 254, 255, corresponding to 1 to 8 mask bits in that octet. Mask 255.255.248.0 = /21 (8 + 8 + 5 bits), so 11 host bits and 2046 usable hosts.
- Borrowing from a classful network: Class B 130.1.0.0 with mask 255.255.248.0 borrows 5 bits, giving 2^5 = 32 subnets of 2^11 - 2 = 2046 hosts each. Under the old rule that excluded the all-0s and all-1s subnets the count would be 30; CIDR-era questions normally allow all 32 unless the question says otherwise.
- DIRECTED BROADCAST for a subnet = network address with all host bits 1. A packet to it is delivered to every host in that subnet; the LIMITED broadcast 255.255.255.255 is never forwarded by routers.
| Prefix | Mask (last non-255 octet) | Block size | Usable hosts |
|---|---|---|---|
| /24 | 255.255.255.0 | 256 | 254 |
| /25 | 255.255.255.128 | 128 | 126 |
| /26 | 255.255.255.192 | 64 | 62 |
| /27 | 255.255.255.224 | 32 | 30 |
| /28 | 255.255.255.240 | 16 | 14 |
| /29 | 255.255.255.248 | 8 | 6 |
| /30 | 255.255.255.252 | 4 | 2 |
Worked example
Problem. For the host 172.16.45.200/20 find the mask, network address, broadcast address and usable hosts. Then split 192.168.1.0/24 for networks of 100, 50 and 20 hosts using VLSM.
Solution. /20 gives mask 255.255.240.0, block size 16 in the third octet. 45 lies in 32 to 47, so network = 172.16.32.0, broadcast = 172.16.47.255, usable range 172.16.32.1 to 172.16.47.254, hosts = 2^12 - 2 = 4094. VLSM: 100 hosts need /25 (126 usable) -> 192.168.1.0/25; 50 hosts need /26 (62) -> 192.168.1.128/26; 20 hosts need /27 (30) -> 192.168.1.192/27. More practice: 200.100.50.77/27 has block size 32, 77 lies in 64 to 95, so network 200.100.50.64, broadcast 200.100.50.95, usable 200.100.50.65 to .94 (30 hosts). 10.20.130.5/18 has mask 255.255.192.0, block 64 in the third octet, 130 lies in 128 to 191, so network 10.20.128.0, broadcast 10.20.191.255, hosts 2^14 - 2 = 16,382. Splitting 192.168.10.0/24 into four equal subnets borrows 2 bits (/26): networks .0, .64, .128, .192 with broadcasts .63, .127, .191, .255 and 62 hosts each.
CIDR, aggregation and longest-prefix match
- CIDR (Classless Inter-Domain Routing) removes class boundaries: an address block is written a.b.c.d/n with any n, so allocation matches real needs and the routing table can use ROUTE AGGREGATION (supernetting).
- AGGREGATION: 2^k contiguous blocks that are properly aligned (the first block starts at a multiple of the combined size) merge into one prefix with k fewer bits. Example: 200.1.0.0/24, 200.1.1.0/24, 200.1.2.0/24 and 200.1.3.0/24 aggregate to 200.1.0.0/22.
- LONGEST-PREFIX MATCH: when several table entries match a destination, the router uses the entry with the LONGEST prefix (most specific route). The default route 0.0.0.0/0 matches everything and is used only if nothing else does.
- A block of size 2^h starting at address X is a valid CIDR block only if X is a multiple of 2^h. Total addresses in a /n block = 2^(32 - n).
- IPv6: 128-bit addresses written as eight 16-bit hex groups (:: replaces one run of zero groups), fixed 40-byte header, no header checksum, no router fragmentation (end hosts use Path MTU discovery), and no broadcast (uses multicast and anycast). Transition uses dual stack and tunnelling.
Formulas to remember
-
Addresses in /n = 2^(32 - n); usable = 2^(32 - n) - 2 -
Network = IP AND mask; Broadcast = Network OR (NOT mask) -
Block size in the interesting octet = 256 - mask octet -
Aggregating 2^k aligned blocks of /n gives one /(n - k)
Worked example
Problem. A router has: 10.0.0.0/8 -> A, 10.1.0.0/16 -> B, 10.1.2.0/24 -> C, default -> D. Where are packets for 10.1.2.77, 10.1.9.9, 10.200.1.1 and 11.0.0.1 sent?
Solution. 10.1.2.77 matches /8, /16 and /24, so the longest prefix gives C. 10.1.9.9 matches /8 and /16, so B. 10.200.1.1 matches only /8, so A. 11.0.0.1 matches nothing but the default route, so D. Second table: 135.46.56.0/22 -> if0, 135.46.60.0/22 -> if1, 192.53.40.0/23 -> R1, default -> R2. Then 135.46.63.10 (third octet 63 = 00111111, top six bits 001111) -> if1; 135.46.57.14 (57 = 00111001, top six bits 001110) -> if0; 135.46.52.2 -> default R2; 192.53.40.7 -> R1; 192.53.56.7 -> R2.
Design patterns and GATE traps in addressing
- AGGREGATION CHECK: n blocks can merge into one prefix only if n is a power of 2, all blocks have the same size, they are contiguous and the first block address is a multiple of the combined size. 150.2.4.0/24 to 150.2.7.0/24 (4 blocks, start 4 is a multiple of 4) merge into 150.2.4.0/22. But 200.1.1.0/24 and 200.1.2.0/24 are adjacent yet cannot merge into one /23 because a /23 must start on an even third octet (200.1.0.0 or 200.1.2.0) and 200.1.1.0 starts on an odd one.
- The /22 block 150.2.4.0 covers third-octet values 4 to 7; adding 150.2.8.0/24 gives 5 blocks, which cannot be one prefix. To find the common prefix, write the network addresses in binary and count the shared leading bits.
- VLSM ALLOCATION ORDER: sort the requirements from largest to smallest and place each block at the next address that is a multiple of its own size. This keeps the blocks aligned and leaves one contiguous free area at the end.
- SAME-SUBNET TEST: AND both addresses with the mask; equal results mean the same subnet and direct delivery (ARP for the destination), otherwise the host ARPs for the default gateway. The router interface address must lie in the subnet of the link it serves.
- SUBNET COUNTS ON A LINK: a point-to-point link needs only a /30 (2 usable hosts); /31 is allowed on point-to-point links by modern practice. Remember that the router interfaces also consume host addresses.
- NAT (port address translation) keeps a table (private IP, private port) <-> (public IP, public port) so thousands of hosts share one public address; it breaks end-to-end addressing but conserves address space. About 65,000 simultaneous mappings per public address are possible because ports are 16 bits.
| Question type | Method | Quick result |
|---|---|---|
| Network and broadcast of host/prefix | block size = 256 - mask octet | 77/27: net .64, bcast .95 |
| Hosts for a mask | 2^(32 - prefix) - 2 | /21 gives 2046, /26 gives 62 |
| Smallest prefix for H hosts | host bits h = ceil(log2(H + 2)) | 500 hosts: h = 9, /23 |
| Subnets from borrowing b bits | 2^b | 5 bits: 32 subnets |
| Aggregate k aligned blocks | prefix shortens by log2 k | 4 x /24 gives /22 |
| Which route is used | longest matching prefix | /24 beats /16 beats /8 |
Worked example
Problem. Allocate subnets from 172.20.0.0/22 for LANs of 500, 120, 60 and 10 hosts using VLSM, and check the space left.
Solution. Sort: 500 needs 9 host bits (510 usable) so /23: 172.20.0.0/23 (172.20.0.0 to 172.20.1.255). 120 needs 7 host bits (126 usable) so /25: 172.20.2.0/25 (172.20.2.0 to .127). 60 needs 6 host bits (62 usable) so /26: 172.20.2.128/26 (.128 to .191). 10 needs 4 host bits (14 usable) so /28: 172.20.2.192/28 (.192 to .207). Used = 512 + 128 + 64 + 16 = 720 addresses of 1024; the free space is 172.20.2.208 to 172.20.3.255 (304 addresses). Each block starts at a multiple of its own size.
Exam tips
- Usable hosts = 2^h - 2; the network and broadcast addresses are never assigned to hosts.
- Use block size = 256 - mask octet; network = largest multiple of block size not above the address.
- VLSM: sort requirements in decreasing order and allocate aligned blocks; each block starts at a multiple of its size.
- CIDR aggregation needs contiguous, aligned, equal-size blocks; the result prefix is shorter by log2(count).
- Longest-prefix match wins over default and shorter routes, regardless of table order.
10 GATE-style MCQs for IP Addressing & Subnetting with full explanations. Attempt each question before revealing the answer.
The IP address 150.10.5.1 belongs to which class?
Show answer & explanation
Answer: B
First byte 150 lies in 128 to 191, so it is a Class B address.
The number of usable host addresses in a /26 network is:
Show answer & explanation
Answer: B
Host bits = 32 - 26 = 6; usable = 2^6 - 2 = 62.
The network address of the host 172.16.45.200/20 is:
Show answer & explanation
Answer: B
Mask 255.255.240.0 gives block size 16 in the third octet; 45 lies in the block starting at 32, so the network is 172.16.32.0.
The directed broadcast address of the subnet containing 192.168.10.100/26 is:
Show answer & explanation
Answer: B
Block size 64: 100 lies in 64 to 127, so the broadcast address is 192.168.10.127.
A subnet must support 500 hosts. The smallest subnet mask that works is:
Show answer & explanation
Answer: B
9 host bits give 2^9 - 2 = 510 >= 500 (8 bits give only 254), so the prefix is /23 = 255.255.254.0.
The prefixes 200.1.0.0/24, 200.1.1.0/24, 200.1.2.0/24 and 200.1.3.0/24 aggregate into:
Show answer & explanation
Answer: B
Four aligned contiguous /24 blocks merge into one /22 (4 = 2^2 blocks, 24 - 2 = 22).
A router has the entries 10.0.0.0/8 -> A, 10.1.0.0/16 -> B, 10.1.2.0/24 -> C and default -> D. A packet for 10.1.2.77 is forwarded to:
Show answer & explanation
Answer: C
The /24 entry is the longest matching prefix.
A Class C network 192.168.1.0 is subnetted by borrowing 3 host bits. The number of usable hosts per subnet is:
Show answer & explanation
Answer: C
8 - 3 = 5 host bits remain; 2^5 - 2 = 30 hosts per subnet (8 subnets of /27).
Which of the following is a private IPv4 address?
Show answer & explanation
Answer: C
The private range 172.16.0.0/12 covers 172.16.0.0 to 172.31.255.255, so 172.20.5.5 is private.
Hosts 10.1.1.130 and 10.1.1.100 both use mask 255.255.255.128. They are:
Show answer & explanation
Answer: B
130 AND 128 = 128 but 100 AND 128 = 0, so the networks are 10.1.1.128 and 10.1.1.0 — different subnets.
Extra page-specific practice beyond the GATE samples. Attempt all 9, then reveal answers to check your reasoning.
The smallest prefix that can accommodate 100 hosts is:
Show answer & explanation
Answer: B) /25
7 host bits give 126 usable hosts (6 bits give only 62), so the prefix is /25.
A Class C network needs at least 5 subnets. The subnet mask is:
Show answer & explanation
Answer: B) 255.255.255.224
Five subnets need 3 borrowed bits (2^3 = 8 >= 5), giving /27 = 255.255.255.224.
The last address (broadcast) of the block 20.30.40.0/21 is:
Show answer & explanation
Answer: C) 20.30.47.255
/21 means block size 8 in the third octet: 40 to 47, so the broadcast is 20.30.47.255.
The total number of addresses (including network and broadcast) in the block 130.5.0.0/18 is:
Show answer & explanation
Answer: C) 16384
2^(32 - 18) = 2^14 = 16384.
The maximum number of host addresses in a Class A network is:
Show answer & explanation
Answer: C) 16,777,214
24 host bits give 2^24 - 2 = 16,777,214 hosts.
An ISP owns 150.20.0.0/16 and splits it into 4 equal subnets. The second subnet is:
Show answer & explanation
Answer: C) 150.20.64.0/18
Four subnets need 2 extra bits (/18), each of size 64 in the third octet: 0, 64, 128, 192. The second starts at 64.
A router has 192.168.0.0/16 -> R1, 192.168.4.0/24 -> R2, 192.168.4.0/22 -> R3 and default -> R4. A packet for 192.168.4.9 goes to:
Show answer & explanation
Answer: B) R2
All three prefixes match; the /24 is the longest, so R2 is chosen.
The IPv6 base header has a fixed size of:
Show answer & explanation
Answer: C) 40 bytes
The IPv6 header is fixed at 40 bytes with extension headers for options.
NAT lets several private hosts share one public IP address mainly by:
Show answer & explanation
Answer: B) translating source address and port numbers and keeping a translation table
PAT/NAT rewrites the private source IP and port to the public IP and a unique port and reverses the mapping for replies.
Topic FAQs
How do I find the network address from an IP and a mask quickly?
AND the address with the mask. For the interesting octet use block size = 256 - mask octet and pick the largest multiple of the block size that does not exceed the address octet.
Why do we subtract 2 when counting hosts?
The all-zeros host part is the network address and the all-ones host part is the directed broadcast address; neither can be assigned to a host interface.
What is the difference between subnetting and supernetting?
Subnetting borrows host bits to split one network into smaller subnets. Supernetting (CIDR aggregation) merges several contiguous networks into one shorter prefix to shrink routing tables.
What does longest-prefix matching mean?
When a destination matches several routing entries, the router uses the entry whose prefix length is the greatest, because it is the most specific route.
Hover a card to reveal the answer with a 3D flip. On touch devices, tap the card or use Show answer.