Cyber Security & Networking8 min read

Demystifying IPv4 Subnetting & CIDR: Calculation Formulas, Masking, and Practical IP Planning

A comprehensive technical engineering guide on how IPv4 subnetting and Classless Inter-Domain Routing (CIDR) work under the hood, with binary mathematics, wildcard masks, and network design examples.

M

Mega Toolskit Technical Team

Network Infrastructure & Security Specialists•Updated 2026-03-01

Introduction to IPv4 Subnetting

In modern computer networking, an IPv4 address is a 32-bit numeric identifier assigned to every device connected to an Internet Protocol network. Because 32 bits yield approximately 4.29 billion possible addresses, network engineers divide large blocks of addresses into smaller, logically organized partitions called subnets (sub-networks).

Subnetting provides three indispensable architectural benefits:

  1. Minimizing Broadcast Storms: Splitting large networks reduces the blast radius of layer-2 ARP broadcasts and multicast traffic.
  2. Access Control & Security Isolation: By isolating departments, database tiers, and public DMZs into discrete subnets, administrators can enforce strict firewall and routing rules between segments.
  3. Conservation of Address Space: Prior to CIDR, the rigid Class A, B, and C hierarchy wasted millions of public IP addresses. Subnetting enables variable-length subnet masking (VLSM) tailored to precise client counts.

The Binary Anatomy of an IPv4 Address

An IPv4 address consists of 4 octets separated by dots, where each octet is 8 bits (1 byte) ranging from 0 to 255.

For example, the private IP address 192.168.1.55 is represented in binary as:

11000000 . 10101000 . 00000001 . 00110111

A subnet mask also consists of 32 bits, divided into a contiguous string of binary 1s (representing the Network portion) followed by binary 0s (representing the Host portion).

When we write CIDR notation like /24, it indicates that the first 24 bits are binary 1s:

Mask:   255.255.255.0
Binary: 11111111 . 11111111 . 11111111 . 00000000 (24 ones, 8 zeros)

Mathematical Derivation of Subnet Metrics

Given any IPv4 address and CIDR prefix length P (where 0 <= P <= 32):

1. Total Number of IP Addresses

The total number of address slots in the subnet block is: $$\text{Total IPs} = 2^{(32 - P)}$$

For a /24 subnet: $$2^{(32 - 24)} = 2^8 = 256 \text{ addresses}$$

2. Usable Host Addresses

In standard IPv4 subnets, two addresses are reserved by RFC standards:

  • Network ID: The first address in the block (all host bits set to 0). It identifies the subnet route itself.
  • Broadcast Address: The final address in the block (all host bits set to 1). Packets sent to this address are received by all hosts in the local segment.

Therefore, for subnets with prefix length $\le 30$: $$\text{Usable Hosts} = 2^{(32 - P)} - 2$$

(Special note on /31 and /32: RFC 3021 permits /31 point-to-point router links without network/broadcast reservations, yielding 2 usable hosts, while /32 designates a single loopback host).


Bitwise Calculation Example: 172.16.45.100 / 20

Let us calculate the network parameters for 172.16.45.100 with a /20 prefix:

  1. Prefix Length: /20 means 20 ones followed by 12 zeros.
    • Netmask: 255.255.240.0
    • Wildcard Mask: Inverse of the netmask = 0.0.15.255
  2. Network Address:
    • Perform bitwise AND between 172.16.45.100 and 255.255.240.0.
    • In the third octet: 45 is 00101101_2 and 240 is 11110000_2.
    • 00101101 AND 11110000 = 00100000_2 = 32.
    • Network ID: 172.16.32.0
  3. Broadcast Address:
    • Perform bitwise OR between Network ID and Wildcard Mask.
    • Third octet: 32 OR 15 = 47. Fourth octet: 0 OR 255 = 255.
    • Broadcast: 172.16.47.255
  4. Usable Range:
    • First Usable Host: 172.16.32.1
    • Last Usable Host: 172.16.47.254
    • Total Usable Hosts: $2^{12} - 2 = 4096 - 2 = \mathbf{4,094}$.

Common CIDR Reference Cheat Sheet

CIDR PrefixSubnet MaskTotal IPsUsable HostsTypical Practical Use Case
/30255.255.255.25242Point-to-point links between enterprise routers
/29255.255.255.24886Small business static IP allocations from ISP
/28255.255.255.2401614Small DMZ, clustered database servers
/27255.255.255.2243230Small departmental office LAN
/26255.255.255.1926462Medium branch department
/24255.255.255.0256254Standard office LAN, home router subnet
/23255.255.254.0512510Corporate campus building floor
/22255.255.252.01,0241,022Large campus Wi-Fi network
/16255.255.0.065,53665,534Large cloud VPC (AWS VPC / GCP VPC)

Best Practices for IP Address Management (IPAM)

  1. Plan for Future Scalability: When provisioning a subnet for office workers or Kubernetes pods, always size the CIDR block to allow at least 50% head-room for future growth without having to re-IP devices.
  2. Reserve Low and High Ends: By convention, assign the first 5 IP addresses in a subnet for gateway routers, DNS servers, and switches, and designate a fixed pool for static servers before DHCP pools begin.
  3. Use the Mega Toolskit Subnet Calculator: Instead of manually computing bitwise binary calculations under pressure, verify network boundaries, broadcast addresses, and host ranges using our free browser-based IP Address & Network Scanner.
Tags:
#Networking#CIDR#IPv4#Subnetting#DevOps#Cyber Security
M

Written by Mega Toolskit Technical Team

Verified Contributor

The Mega Toolskit engineering team researches, benchmarks, and maintains open web tools, cryptographic utilities, and calculation frameworks. All guides follow peer-reviewed industry standards and RFC protocols.