CIDR notation in Computer Networks - Time & Space Complexity
When working with CIDR notation, it is important to understand how the time to process addresses grows as the network size changes.
We want to know how the number of operations changes when calculating or matching IP addresses using CIDR.
Analyze the time complexity of the following code snippet.
function isInSubnet(ip, cidr) {
const [subnet, prefixLength] = cidr.split('/');
const ipBits = ipToBits(ip);
const subnetBits = ipToBits(subnet);
for (let i = 0; i < Number(prefixLength); i++) {
if (ipBits[i] !== subnetBits[i]) {
return false;
}
}
return true;
}
function ipToBits(ip) {
return ip.split('.').map(octet => parseInt(octet).toString(2).padStart(8, '0')).join('');
}
This code checks if an IP address belongs to a subnet defined by CIDR notation by comparing bits up to the prefix length.
- Primary operation: Loop comparing bits of IP and subnet up to prefix length.
- How many times: The loop runs once for each bit in the prefix length (usually between 0 and 32 for IPv4).
The number of operations grows directly with the prefix length, which is the number of bits to compare.
| Input Size (prefix length) | Approx. Operations (bit comparisons) |
|---|---|
| 8 | 8 |
| 16 | 16 |
| 24 | 24 |
Pattern observation: The time grows linearly as the prefix length increases.
Time Complexity: O(n)
This means the time to check if an IP is in a CIDR subnet grows in a straight line with the number of bits compared.
[X] Wrong: "Checking if an IP is in a CIDR subnet takes the same time regardless of prefix length."
[OK] Correct: The code compares each bit up to the prefix length, so longer prefixes mean more comparisons and more time.
Understanding how CIDR matching scales helps you explain network filtering and routing decisions clearly and confidently.
"What if we changed the code to compare bytes instead of bits? How would the time complexity change?"
Practice
192.168.1.0/24.Solution
Step 1: Understand CIDR notation structure
CIDR notation shows an IP address followed by a slash and a number. This number tells how many bits are used for the network part of the address.Step 2: Interpret the number after the slash
The number after the slash (like 24 in 192.168.1.0/24) means the first 24 bits are the network portion, defining the network size.Final Answer:
The size of the network or how many bits are used for the network part -> Option BQuick Check:
Number after slash = network bits [OK]
- Confusing slash number with total hosts
- Thinking slash number is IP version
- Assuming slash number shows number of hosts
Solution
Step 1: Convert subnet mask to CIDR prefix length
Subnet mask 255.255.255.0 means the first 24 bits are ones (11111111.11111111.11111111.00000000), so the CIDR prefix is /24.Step 2: Match IP with correct CIDR notation
Given IP 10.0.0.0 with subnet mask 255.255.255.0 corresponds to 10.0.0.0/24.Final Answer:
10.0.0.0/24 -> Option CQuick Check:
255.255.255.0 = /24 [OK]
- Confusing /16 with 255.255.0.0 mask
- Using /32 which means single IP
- Mixing up subnet mask and CIDR prefix
192.168.10.0/28, how many usable IP addresses are available in this subnet?Solution
Step 1: Calculate total IP addresses from CIDR prefix
/28 means 28 bits for network, so 32 - 28 = 4 bits for hosts. Total IPs = 2^4 = 16.Step 2: Calculate usable IP addresses
Usable IPs exclude network and broadcast addresses, so usable = 16 - 2 = 14.Final Answer:
14 -> Option AQuick Check:
Usable IPs = 2^(32-28) - 2 = 14 [OK]
- Counting total IPs as usable
- Forgetting to subtract network and broadcast addresses
- Miscalculating bits for hosts
172.16.0.0/33. What is wrong with this notation?Solution
Step 1: Understand CIDR prefix limits for IPv4
IPv4 addresses are 32 bits long, so CIDR prefix must be between 0 and 32 inclusive.Step 2: Identify invalid prefix
A prefix of 33 is invalid because it exceeds the 32-bit length of IPv4 addresses.Final Answer:
CIDR prefix cannot be greater than 32 for IPv4 -> Option DQuick Check:
IPv4 prefix max = 32 [OK]
- Thinking IP address is invalid instead
- Confusing CIDR prefix with subnet mask
- Assuming prefix must be less than 16
Solution
Step 1: Calculate required host bits for 1000 hosts
Hosts = 2^(host bits) - 2. We want at least 1000 usable hosts. Try host bits: 10 bits gives 2^10 - 2 = 1022 hosts, which is enough.Step 2: Calculate CIDR prefix from host bits
IPv4 has 32 bits total. Network bits = 32 - host bits = 32 - 10 = 22. So /22 supports 1022 hosts.Step 3: Check options for smallest network
/22 supports 1022 hosts, /23 supports 510 hosts (too small), /21 supports 2046 hosts (larger than needed), /24 supports 254 hosts (too small). Smallest that fits is /22.Final Answer:
192.168.0.0/22 -> Option AQuick Check:
Hosts ≥ 1000 -> /22 (1022 hosts) [OK]
- Choosing /23 or /24 which are too small
- Not subtracting 2 for network and broadcast
- Picking /21 which is larger than needed
Ready to prove it?
Pass the test and this pattern moves unknown
