Skip to main content

Subnetting: Network, Broadcast & Host Range Calculation

Medium

Interview Question: "Given the IP address 192.168.10.75 with subnet mask 255.255.255.192 (/26), determine the network address, broadcast address, range of valid host addresses, and total usable hosts. Show both the formal binary breakdown and the rapid 'Magic Number' interview shortcut."


The setup​

Subnetting just means carving one big IP block into smaller, isolated chunks — smaller broadcast domains, cleaner firewall boundaries, less wasted address space.

Problem Parameters:​

  • Given IPv4 Address: 192.168.10.75
  • Subnet Mask: 255.255.255.192
  • CIDR Prefix Length: /26 (8+8+8+2=268 + 8 + 8 + 2 = 26 network bits)
  • The "Interesting Octet": Octet 4 (75 and 192). The first three octets (255.255.255) contain all 1s, meaning the network prefix 192.168.10. remains completely locked and unchanged.

Method 1: The Formal Binary Bitwise Breakdown​

In academic and deep-dive technical interviews, you are expected to demonstrate how network hardware (routers and L3 switches) evaluates addresses at the bit level.

Step 1: Convert the 4th Octet to Binary​

  • IP Octet 4 (75): 0100 1011 (64+8+2+164 + 8 + 2 + 1)
  • Mask Octet 4 (192): 1100 0000 (128+64128 + 64)

The first 2 bits (11) belong to the network prefix. The remaining 6 bits (000000) are allocated for host addressing (h=6h = 6).

Step 2: Bitwise AND to Find Network Address​

The router applies a bitwise AND operation between the IP address and the subnet mask. Any bit ANDed with 0 becomes 0, zeroing out the host portion:

IP (4th Octet): 0 1 0 0 1 0 1 1 (75)
Mask (4th Octet): & 1 1 0 0 0 0 0 0 (192)
-----------------------------------
Network Result: 0 1 0 0 0 0 0 0 (= 64 in decimal)

Network Address: 192.168.10.64\mathbf{\text{Network Address: } 192.168.10.64}

Step 3: Set Host Bits to 1 to Find Broadcast Address​

The directed broadcast address addresses every host on the subnet. It is generated by setting all 6 host bits to 1s:

Network Bits: 0 1 _ _ _ _ _ _
Host Bits to 1: 0 1 1 1 1 1 1 1 (= 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127)

Broadcast Address: 192.168.10.127\mathbf{\text{Broadcast Address: } 192.168.10.127}

Step 4: Calculate Valid Host Range and Capacity​

  • First Usable Host: Network Address + 1   ⟹  \implies 192.168.10.65
  • Last Usable Host: Broadcast Address - 1   ⟹  \implies 192.168.10.126
  • Total Usable Hosts Formula: Usable Hosts=2h−2\text{Usable Hosts} = 2^h - 2 Usable Hosts=26−2=64−2=62 Usable Hosts\text{Usable Hosts} = 2^6 - 2 = 64 - 2 = \mathbf{62\text{ Usable Hosts}}

Why Subtract 2? The first address in the block (all host bits 0) represents the Network Identifier itself, and the final address (all host bits 1) is reserved for the Directed Broadcast. Neither can be assigned to a host NIC.


Method 2: The 5-Second "Magic Number" Shortcut​

Converting decimal to binary takes too long on a whiteboard. Network engineers skip it entirely with the Magic Number trick:

  1. Find the "interesting octet" — the one that isn't 255 or 0. Here, that's octet 4: mask value 192.
  2. Magic Number = 256 − mask octet = 256 − 192 = 64.
  3. List multiples of 64, starting at 0: 0, 64, 128, 192...
  4. Find where the IP (75) lands: it's between 64 and 128.
192.168.10.64 – .12764
192.168.10.128 – .19164
64128192

The IP .75 falls inside the first block, so:

  • Network Address: 192.168.10.64 (lower boundary)
  • Broadcast Address: 192.168.10.127 (one below the next subnet's boundary, .128)
  • First Usable Host: 192.168.10.65 (network + 1)
  • Last Usable Host: 192.168.10.126 (broadcast − 1)

Fast-Reference CIDR Subnet Cheat Sheet (/24 to /30)​

CIDR PrefixSubnet MaskHost Bits (hh)Magic Number (Block Size)Total IPs (2h2^h)Usable Hosts (2h−22^h - 2)Primary Use Case
/24255.255.255.08256256254Standard small office / LAN.
/25255.255.255.1287128128126Half-C department network.
/26255.255.255.1926646462Quarter-C team subnet.
/27255.255.255.2245323230Small branch or server cluster.
/28255.255.255.2404161614DMZ or management subnet.
/29255.255.255.2483886Small public static IP block.
/30255.255.255.2522442Point-to-Point WAN router links.

Interview Traps & RFC Edge Cases​

  1. RFC 3021 (/31 Subnets for Point-to-Point Links):
    • In traditional networking, a /31 mask has 21−2=02^1 - 2 = 0 usable hosts.
    • However, RFC 3021 allows point-to-point router links to use /31 prefixes (255.255.255.254255.255.255.254) with 2 usable hosts, treating the first address as host 0 and second as host 1 without dedicated network or broadcast addresses. This saves millions of IPv4 addresses on backbone transit links.
  2. Host Routes (/32):
    • A /32 prefix (255.255.255.255255.255.255.255) has 0 host bits and represents a single specific endpoint (e.g., a loopback interface or VPN client tunnel).
  3. Subnetting Across Octet Boundaries (/22, /23):
    • When the mask is in the 3rd octet (e.g. 255.255.252.0   ⟹  \implies /22), the magic number is 256−252=4256 - 252 = 4. Subnet boundaries increment by 4 in the 3rd octet (172.16.0.0, 172.16.4.0, 172.16.8.0...).

Python: Subnet Calculator​

The following executable Python script implements subnet boundary calculations using raw bitwise arithmetic and verifies them against Python's standard ipaddress module:

"""
IPv4 Subnet Calculator & Verification Suite
Computes network address, broadcast address, valid host range, and capacity.
"""
import ipaddress


def calculate_subnet_bitwise(ip_str: str, mask_str: str) -> dict:
ip_parts = [int(x) for x in ip_str.split(".")]
mask_parts = [int(x) for x in mask_str.split(".")]

# Bitwise AND for network address
net_parts = [ip_parts[i] & mask_parts[i] for i in range(4)]
# Invert mask bits for broadcast address
inv_mask = [255 - mask_parts[i] for i in range(4)]
bcast_parts = [net_parts[i] | inv_mask[i] for i in range(4)]

# Format strings
network_addr = ".".join(map(str, net_parts))
broadcast_addr = ".".join(map(str, bcast_parts))

# Host ranges
first_host_parts = net_parts[:]
first_host_parts[3] += 1
last_host_parts = bcast_parts[:]
last_host_parts[3] -= 1

# Total usable hosts
total_zeros = sum(bin(m).count("0") - 2 for m in mask_parts) # Count zero bits
# Compensate for 8-bit octet formatting
zero_bits = sum(8 - bin(m).count("1") for m in mask_parts)
usable_hosts = (2 ** zero_bits) - 2

return {
"network": network_addr,
"broadcast": broadcast_addr,
"first_host": ".".join(map(str, first_host_parts)),
"last_host": ".".join(map(str, last_host_parts)),
"usable_hosts": usable_hosts,
"host_bits": zero_bits
}


if __name__ == "__main__":
ip_input = "192.168.10.75"
mask_input = "255.255.255.192"

print("=" * 65)
print("IPV4 SUBNETTING CALCULATION VERIFICATION")
print(f"Target IP: {ip_input} | Subnet Mask: {mask_input}")
print("=" * 65)

# 1. Bitwise Algorithm
res = calculate_subnet_bitwise(ip_input, mask_input)
print(f"Network Address: {res['network']}")
print(f"Broadcast Address: {res['broadcast']}")
print(f"Usable Host Range: {res['first_host']} - {res['last_host']}")
print(f"Total Usable Hosts: {res['usable_hosts']} (2^{res['host_bits']} - 2)")

# 2. Standard Library Cross-Verification
net_obj = ipaddress.IPv4Network(f"{ip_input}/{mask_input}", strict=False)
print(f"\n--- Python ipaddress Library Cross-Check ---")
print(f"Network: {net_obj.network_address}")
print(f"Broadcast: {net_obj.broadcast_address}")
print(f"Hosts Available: {net_obj.num_addresses - 2}")

assert str(net_obj.network_address) == res["network"]
assert str(net_obj.broadcast_address) == res["broadcast"]
assert (net_obj.num_addresses - 2) == res["usable_hosts"]
print("\nSUCCESS: All subnetting calculations mathematically verified.")