DEV Community

Kaifi Azam
Kaifi Azam

Posted on

How to Calculate an IPv4 Subnet Range From Any IP Address

How to Calculate an IPv4 Subnet Range From Any IP Address

A CIDR value such as:

192.168.1.130/26
Enter fullscreen mode Exit fullscreen mode

contains two pieces of information:

  • 192.168.1.130 is an address inside the subnet
  • /26 means the first 26 bits identify the network

The entered address does not need to be the network address. You can calculate the containing network from any valid address inside the range.

In this example, the result is:

Network:          192.168.1.128/26
Broadcast:        192.168.1.191
First usable:     192.168.1.129
Last usable:      192.168.1.190
Subnet mask:      255.255.255.192
Wildcard mask:    0.0.0.63
Total addresses:  64
Usable hosts:     62
Enter fullscreen mode Exit fullscreen mode

Here is how to calculate every value manually.

Step 1: Convert the prefix into host bits

IPv4 addresses contain 32 bits.

A /26 prefix reserves 26 bits for the network:

Host bits = 32 - 26
Host bits = 6
Enter fullscreen mode Exit fullscreen mode

The subnet therefore contains:

2⁶ = 64 addresses
Enter fullscreen mode Exit fullscreen mode

For a normal subnet, the lowest address identifies the network and the highest address is the broadcast address.

That leaves:

64 - 2 = 62 usable host addresses
Enter fullscreen mode Exit fullscreen mode

Step 2: Find the subnet mask

A /26 mask contains 26 ones followed by 6 zeros:

11111111.11111111.11111111.11000000
Enter fullscreen mode Exit fullscreen mode

Convert each octet to decimal:

255.255.255.192
Enter fullscreen mode Exit fullscreen mode

So the subnet mask is:

255.255.255.192
Enter fullscreen mode Exit fullscreen mode

Step 3: Calculate the block size

The interesting octet is the first mask octet that is not 255.

For /26, that octet is 192.

Calculate the block size:

Block size = 256 - 192
Block size = 64
Enter fullscreen mode Exit fullscreen mode

The possible subnet boundaries in the final octet are therefore:

0
64
128
192
Enter fullscreen mode Exit fullscreen mode

Each range contains 64 addresses:

0–63
64–127
128–191
192–255
Enter fullscreen mode Exit fullscreen mode

The entered address ends in 130.

Since 130 falls between 128 and 191, its network is:

192.168.1.128/26
Enter fullscreen mode Exit fullscreen mode

Step 4: Find the broadcast address

The broadcast address is the final address in the subnet.

The selected block runs from:

192.168.1.128
Enter fullscreen mode Exit fullscreen mode

through:

192.168.1.191
Enter fullscreen mode Exit fullscreen mode

Therefore:

Broadcast address = 192.168.1.191
Enter fullscreen mode Exit fullscreen mode

Another way to calculate it is:

Broadcast = Network + Total addresses - 1
Broadcast = 128 + 64 - 1
Broadcast = 191
Enter fullscreen mode Exit fullscreen mode

Step 5: Find the usable host range

For a traditional IPv4 subnet:

First usable = Network + 1
Last usable  = Broadcast - 1
Enter fullscreen mode Exit fullscreen mode

That gives:

First usable: 192.168.1.129
Last usable:  192.168.1.190
Enter fullscreen mode Exit fullscreen mode

The same calculation in binary

The final octet of the entered IP is 130:

130 = 10000010
Enter fullscreen mode Exit fullscreen mode

The final octet of the /26 mask is 192:

192 = 11000000
Enter fullscreen mode Exit fullscreen mode

Apply the mask to the address:

IP:       10000010
Mask:     11000000
Network:  10000000
Enter fullscreen mode Exit fullscreen mode

10000000 is 128, giving:

192.168.1.128
Enter fullscreen mode Exit fullscreen mode

Set all six host bits to 1 to find the broadcast address:

Network:    10000000
Broadcast:  10111111
Enter fullscreen mode Exit fullscreen mode

10111111 is 191, giving:

192.168.1.191
Enter fullscreen mode Exit fullscreen mode

Quick CIDR reference

Prefix Subnet mask Total addresses Traditional usable hosts
/24 255.255.255.0 256 254
/25 255.255.255.128 128 126
/26 255.255.255.192 64 62
/27 255.255.255.224 32 30
/28 255.255.255.240 16 14
/29 255.255.255.248 8 6
/30 255.255.255.252 4 2
/31 255.255.255.254 2 Special case
/32 255.255.255.255 1 Single address

The same block-size method works across octet boundaries.

For example, with a /20 mask:

Subnet mask = 255.255.240.0
Block size  = 256 - 240
Block size  = 16
Enter fullscreen mode Exit fullscreen mode

The third-octet boundaries are:

0, 16, 32, 48, 64, 80...
Enter fullscreen mode Exit fullscreen mode

An address such as:

172.16.37.10/20
Enter fullscreen mode Exit fullscreen mode

falls inside the 32–47 block.

Its containing network is therefore:

172.16.32.0/20
Enter fullscreen mode Exit fullscreen mode

and its broadcast address is:

172.16.47.255
Enter fullscreen mode Exit fullscreen mode

The /31 exception

A /31 contains two addresses.

Under the traditional subnet rule, subtracting a network and broadcast address would leave no usable hosts. However, /31 networks can use both addresses as endpoints on a point-to-point link.

For example:

10.0.0.4/31
Enter fullscreen mode Exit fullscreen mode

contains:

10.0.0.4
10.0.0.5
Enter fullscreen mode Exit fullscreen mode

On an appropriately configured point-to-point link, both can be used as endpoint addresses.

Do not automatically treat /31 like a regular LAN subnet.

What does /32 mean?

A /32 identifies one exact IPv4 address:

203.0.113.25/32
Enter fullscreen mode Exit fullscreen mode

It contains no additional addresses and is commonly used when a route, rule, or configuration must match a single host.

Calculate and verify it automatically

For quick checks, I built the Olivez IPv4 Subnet Calculator.

You can enter any IPv4 address with a prefix from /0 through /32, including an address that is not already the network boundary.

It calculates:

  • Containing CIDR network
  • Subnet mask
  • Wildcard mask
  • Broadcast address
  • First and last usable addresses
  • Total and usable address counts
  • Address classification
  • Binary network and host split

For example, entering:

192.168.1.130/26
Enter fullscreen mode Exit fullscreen mode

returns the normalized network:

192.168.1.128/26
Enter fullscreen mode Exit fullscreen mode

The calculator is useful for verifying manual work, but understanding the block-size method makes it much easier to spot incorrect CIDR ranges in routing tables, firewall rules, cloud networks, and infrastructure configuration.

A reusable calculation process

For any IPv4 address and prefix:

  1. Subtract the prefix from 32 to find the number of host bits.
  2. Calculate 2^host bits to find the total number of addresses.
  3. Convert the prefix to a subnet mask.
  4. Find the first mask octet that is not 255.
  5. Calculate its block size using 256 - mask octet.
  6. Find which block contains the entered address.
  7. Use the first address as the network boundary.
  8. Use the final address as the broadcast boundary.
  9. Exclude those two addresses for the traditional usable range.
  10. Handle /31 and /32 as special cases.

Once you can identify the block boundary, the rest of the subnet becomes straightforward.

Top comments (0)