IP Address & CIDR Subnet Calculator
IPv4 addresses are 32-bit unsigned integers disguised in dot-decimal notation. Classless Inter-Domain Routing (CIDR) applies a bitmask of prefix length to partition networks. Calculating netmasks, network roots, and host membership requires pure bitwise AND, OR, and NOT operations.
# Convert dot-decimal string to 32-bit int:
parts = [int(p) for p in ip.split('.')]
ip_int = (parts[0]<<24) | (parts[1]<<16) | (parts[2]<<8) | parts[3]
# IP Address & CIDR Subnet Calculator When configuring virtual private clouds (VPCs), Kubernetes pod networks, or firewall security groups, treating IP addresses as string literals is slow and error-prone. At the hardware level, an IPv4 address is simply a **32-bit unsigned integer**. 🌐🔢 **CIDR notation** (`192.168.1.100/24`) pairs an IP with a prefix length indicating how many leading bits belong to the network prefix: - **Netmask**: $N$ leading 1s followed by $32 - N$ zeros. - **Network Address**: `ip_int & mask_int` - **Broadcast Address**: `network_int | (~mask_int & 0xFFFFFFFF)` - **Usable Hosts**: $2^{32 - N} - 2$ (for $N < 31$) - **Contains IP**: `(test_ip & mask_int) == network_int` ```python # Pure bitwise conversion: ip_int = (192 << 24) | (168 << 16) | (1 << 8) | 100 mask_int = ((1 << 32) - 1) ^ ((1 << (32 - 24)) - 1) ``` ## Pop Quiz How is the network address computed given an IP address integer and a subnet mask integer? - [ ] By executing a bitwise AND between the IP integer and the netmask integer (`ip & mask`). - [ ] By shifting the IP integer right by 8 bits and XORing with the broadcast address. - [ ] By dividing the 32-bit integer by 256 and rounding down to the nearest multiple of 4. - [ ] By concatenating the first two octets with the router default gateway. ## Real-World Challenge Implement `CIDRCalculator(cidr: str)`. Expose properties: - `network_address -> str` - `broadcast_address -> str` - `netmask -> str` - `usable_hosts -> int` - `contains(ip: str) -> bool`: Returns `True` if the provided IP belongs to this subnet. <details> <summary>Solution</summary> ```python def ip_to_int(ip_str: str) -> int: parts = [int(p) for p in ip_str.split(".")] return (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8) | parts[3] def int_to_ip(val: int) -> str: return f"{(val >> 24) & 0xFF}.{(val >> 16) & 0xFF}.{(val >> 8) & 0xFF}.{val & 0xFF}" class CIDRCalculator: def __init__(self, cidr: str): ip_str, prefix_str = cidr.split("/") self.prefix = int(prefix_str) self.ip_int = ip_to_int(ip_str) if self.prefix == 0: self.mask_int = 0 else: self.mask_int = ((1 << 32) - 1) ^ ((1 << (32 - self.prefix)) - 1) self.network_int = self.ip_int & self.mask_int self.broadcast_int = self.network_int | (~self.mask_int & 0xFFFFFFFF) @property def network_address(self) -> str: return int_to_ip(self.network_int) @property def broadcast_address(self) -> str: return int_to_ip(self.broadcast_int) @property def netmask(self) -> str: return int_to_ip(self.mask_int) @property def usable_hosts(self) -> int: if self.prefix >= 31: return 0 return (1 << (32 - self.prefix)) - 2 def contains(self, test_ip: str) -> bool: t_int = ip_to_int(test_ip) return (t_int & self.mask_int) == self.network_int # Verification calc = CIDRCalculator("10.0.10.45/24") assert calc.network_address == "10.0.10.0" assert calc.broadcast_address == "10.0.10.255" assert calc.netmask == "255.255.255.0" assert calc.usable_hosts == 254 assert calc.contains("10.0.10.1") is True assert calc.contains("10.0.10.254") is True assert calc.contains("10.0.11.1") is False ``` </details>