How to Calculate a Subnet Mask from an IP Range

Understanding how to derive a subnet mask from a given IP range is one of those networking fundamentals that unlocks a lot of clarity about how networks are actually designed. Whether you're setting up a home lab, configuring a router, or studying for a networking certification, the math here is consistent — and once you see the logic, it sticks.

What a Subnet Mask Actually Does

A subnet mask tells a device which part of an IP address identifies the network and which part identifies individual hosts (devices) on that network. It works alongside an IP address to define the boundaries of a subnet.

For example, the subnet mask 255.255.255.0 paired with IP 192.168.1.10 means:

  • Network portion:192.168.1
  • Host portion:.10

Every IP address and subnet mask is 32 bits long. The mask uses consecutive 1s to mark the network bits, then 0s for the host bits. That boundary point — written in shorthand as /24, /16, etc. — is the CIDR prefix length.

The Core Calculation: Working from a Range to a Mask

If you're given a range of IP addresses and need to find the correct subnet mask, the process breaks down into a few steps.

Step 1 — Determine How Many Hosts You Need

Count the total number of usable IP addresses in the range. Remember:

  • Every subnet loses 2 addresses — one for the network address (first IP) and one for the broadcast address (last IP).
  • So a range of 192.168.1.0 to 192.168.1.255 contains 256 total addresses, but only 254 usable host addresses.

Step 2 — Find the Nearest Power of 2

Subnet sizes must be powers of 2. Common sizes:

Total AddressesUsable HostsCIDR PrefixSubnet Mask
20/31255.255.255.254
42/30255.255.255.252
86/29255.255.255.248
1614/28255.255.255.240
3230/27255.255.255.224
6462/26255.255.255.192
128126/25255.255.255.128
256254/24255.255.255.0
512510/23255.255.254.0
10241022/22255.255.252.0

Take your required host count, add 2 (for network + broadcast), then round up to the nearest power of 2. That gives you the block size.

Step 3 — Convert Block Size to Prefix Length

Once you have the block size, the CIDR prefix length is:

Prefix = 32 − log₂(block size)

For example, if you need 50 hosts:

  • 50 + 2 = 52 → round up to 64
  • 32 − log₂(64) = 32 − 6 = /26
  • Subnet mask: 255.255.255.192

Step 4 — Verify the Range Fits

Check that your starting IP aligns with the subnet boundary. A /26 subnet with 64 addresses has valid starting points at .0, .64, .128, and .192 within the last octet. If your IP range starts at 192.168.1.64 and ends at 192.168.1.127, that's a clean /26. If the range doesn't align to a valid boundary, the mask won't work correctly.

🔢 The Binary View (Why the Math Works)

Each bit in a subnet mask represents a doubling of the address space. Moving from /24 to /23 adds one more host bit, which doubles the number of addresses from 256 to 512. This binary structure means you can't create a subnet for exactly 100 hosts — the next valid size is 128 total addresses (/25), giving 126 usable hosts.

The last octet math for common masks:

  • /25 → 128 = 10000000 → mask last octet = 10000000 = 128
  • /26 → 64 = 01000000 → mask last octet = 11000000 = 192
  • /27 → 32 → mask last octet = 11100000 = 224
  • /28 → 16 → mask last octet = 11110000 = 240

Variables That Change the Outcome

The "right" subnet mask isn't just a math problem — several real-world factors shape what actually makes sense:

  • Network growth: Are you calculating for current hosts only, or planning for future expansion? A /26 that fits 50 hosts today has no room to grow to 70 hosts without renumbering.
  • Contiguous vs. non-contiguous ranges: Some legacy configurations use non-contiguous masks, but modern networks almost exclusively use CIDR-based contiguous masks.
  • IPv4 vs. IPv6: This calculation applies to IPv4. IPv6 subnetting uses the same prefix logic but operates on 128-bit addresses, so the numbers scale differently.
  • Router and device support: Most modern equipment handles VLSM (Variable Length Subnet Masking) natively, but older hardware may only support classful subnets (Class A /8, Class B /16, Class C /24).
  • Private vs. public address space: The calculation mechanics are identical, but which address ranges you're working with affects routing behavior and whether addresses are globally routable.

When the Range Spans Multiple Subnets 🌐

Sometimes an IP range doesn't map cleanly to a single subnet. A range like 10.0.0.0 to 10.0.0.200 cannot be covered by one subnet without including extra addresses — the closest single mask would be /24 (covering .0 to .255). In cases like this, network engineers use CIDR aggregation or split the range into multiple smaller subnets, each with its own mask.

Tools like ipcalc, SolarWinds Subnet Calculator, or built-in OS utilities can verify your calculations and flag boundary misalignments quickly.

The calculation itself is deterministic — given a range, there's always a mathematically correct answer. Whether that answer fits your network's architecture, addressing policy, and capacity planning goals is where the purely technical answer ends and your specific situation begins.