Is an IP Inside a CIDR Block
Problem Statement
Determine whether an IPv4 address falls within a CIDR block.
Background
A CIDR network/prefix covers all addresses sharing the top prefix bits with the network address. Build a mask of prefix ones followed by 32-prefix zeros; the IP is in-range if (ip & mask) == (network & mask).
Your Task
def in_cidr(ip, cidr):
- ip: dotted-quad string.
- cidr: "network/prefix" string.
- Return True if ip is within the block.
Input Format
- ip (str), cidr (str).
Output Format
- A boolean.
Sample
print(in_cidr("10.0.5.9", "10.0.0.0/16"))
Output:
True
Example:
print(in_cidr("10.0.5.9", "10.0.0.0/16"))True
- Parse the CIDR string
"10.0.0.0/16"to extract the network address10.0.0.0and the prefix length 16. - Convert the IP address
"10.0.5.9"and the network address"10.0.0.0"into 32-bit integers:- IP: 10â‹…2563+0â‹…2562+5â‹…256+9=167773449
- Network: 10â‹…2563+0â‹…2562+0â‹…256+0=167772160
- Construct the subnet mask with 16 leading ones and 16 trailing zeros, which equals 232−216=4294901760 (or 0xFFFF0000 in hex).
- Apply the bitwise AND operation to both the IP and the network address using the mask to isolate the network portion:
- IP masked: 167773449&4294901760=167772160
- Network masked: 167772160&4294901760=167772160
- Compare the masked values; since 167772160==167772160, the IP falls within the CIDR block.
- The final output is True
Constraints:
- Mask =
(0xFFFFFFFF << (32-prefix)) & 0xFFFFFFFF. - In-range iff masked ip equals masked network.
prefixin0..32.
1. Background Knowledge
CIDR (Classless Inter-Domain Routing) notation, written as network/prefix, defines a contiguous range of IPv4 addresses. The prefix value (0–32) indicates how many leading bits are fixed. All addresses sharing those top prefix bits belong to the block. For example, 10.0.0.0/16 fixes the first 16 bits, leaving the remaining 16 bits free, which yields 216=65536 possible addresses.
An IPv4 address is a 32-bit number. A subnet mask is a 32-bit value with prefix ones followed by 32 - prefix zeros. To test membership, you apply the mask to both the candidate IP and the network address using a bitwise AND. If the results are equal, the IP is inside the block. This works because the AND operation zeroes out the host bits, leaving only the network portion for comparison.
Converting between dotted-quad strings (e.g., "10.0.5.9") and 32-bit integers is the core mechanical step. Each octet contributes 8 bits: the first octet is shifted left by 24, the second by 16, the third by 8, and the fourth by 0.
2. Algorithm Approach
The solution follows a bitwise masking pattern:
- Parse the dotted-quad IP into a single 32-bit integer.
- Parse the CIDR string into a network integer and a prefix length.
- Construct the subnet mask from the prefix length.
- Compare (ip_int & mask) == (network_int & mask).
This is an O(1) operation once parsing is complete, since all steps involve fixed-width integer arithmetic.
3. Step-by-Step Strategy
Continue the full explanation
You're reading the free preview. Unlock the complete walkthrough, the code editor, test runner and reference solution with Premium.
Editor locked
The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.