Start with the number of addresses you need
To calculate a subnet mask from an IP range, you first count how many individual addresses fall between your starting and ending IP. The subnet mask size depends entirely on this count — not on the IP addresses themselves.
If your range is 192.168.1.0 to 192.168.1.255, that is 256 addresses. If your range is 10.0.0.0 to 10.0.0.31, that is 32 addresses. Write down the exact number before you move forward, because the math flows from this single figure.
Key Takeaways
- Count the total number of addresses in your range by subtracting the first address from the last and adding one.
- Convert that count to a power of 2 — if you need 50 addresses, round up to 64, which is 2 to the 6th power.
- Subtract the power from 32 to find the prefix length, then convert that to dotted-decimal notation for the subnet mask.
- The starting IP must be aligned to the subnet boundary, or your range will not match the mask you calculate.
- Online subnet calculators can verify your math, but understanding the steps helps you spot when a range does not fit a clean subnet.
Convert your address count to a power of 2
Subnet masks work in powers of 2. The usable addresses in a subnet are always 2 raised to some power: 2, 4, 8, 16, 32, 64, 128, 256, and so on. If your count does not land exactly on a power of 2, round up to the next one.
If you need 50 addresses, round up to 64 (which is 2 to the 6th power). If you need 100 addresses, round up to 128 (which is 2 to the 7th power). If you need 200 addresses, round up to 256 (which is 2 to the 8th power). Write down which power of 2 you landed on — you will use this number next.
Calculate the prefix length from the power
An IPv4 address has 32 bits total. The prefix length (also called the CIDR notation) tells you how many of those 32 bits belong to the network, and the rest belong to individual hosts.
Subtract your power from 32. If you rounded up to 64 addresses (2 to the 6th power), subtract 6 from 32 to get 26. If you rounded up to 256 addresses (2 to the 8th power), subtract 8 from 32 to get 24. This result is your prefix length. A /26 network holds 64 addresses. A /24 network holds 256 addresses.
Convert the prefix length to dotted-decimal notation
The prefix length is shorthand. To write out the subnet mask in the traditional four-part format (like 255.255.255.0), you convert the prefix to binary, then to decimal.
Each of the four parts of an IP address represents 8 bits. A /24 prefix means the first 24 bits are the network, so that is 8 + 8 + 8 = three complete octets of all 1s, which converts to 255.255.255.0. A /26 prefix means 24 bits plus 2 more bits, so the fourth octet is 11000000 in binary, which is 192 in decimal — giving you 255.255.255.192. A /25 prefix gives you 255.255.255.128 (because 10000000 in binary is 128).
Common conversions: /30 is 255.255.255.252 (for 4 addresses), /27 is 255.255.255.224 (for 32 addresses), /28 is 255.255.255.240 (for 16 addresses). If you work with larger networks, /16 is 255.255.0.0 and /8 is 255.0.0.0.
Verify that your starting IP aligns to the subnet boundary
The starting IP in your range must be aligned to the subnet mask you calculated. This means the host portion of the address (the bits not covered by the mask) must be all zeros.
If your subnet mask is 255.255.255.192 (/26), the valid starting addresses are 0, 64, 128, and 192 in the fourth octet. So 192.168.1.0, 192.168.1.64, 192.168.1.128, and 192.168.1.192 are all valid starting points. But 192.168.1.50 is not — it falls in the middle of a subnet and cannot be a network address.
To check alignment: convert the starting IP's host portion to binary and confirm it is all zeros. If it is not, your range does not fit a standard subnet, and you will need to either adjust the range or use a larger mask.
Work through a real example
Suppose you need to cover the range 10.20.30.0 to 10.20.30.127. Count the addresses: 127 − 0 + 1 = 128 addresses. The power of 2 that matches 128 is 2 to the 7th power. Subtract 7 from 32 to get a prefix of /25. Convert /25 to dotted decimal: the first three octets are 255.255.255, and the fourth is 10000000 in binary, which is 128 in decimal. Your subnet mask is 255.255.255.128.
Check the alignment: the starting address 10.20.30.0 has a host portion of 0 in the fourth octet, which is all zeros in binary. The alignment is correct. Your answer is /25 or 255.255.255.128, and it covers exactly 128 addresses from 10.20.30.0 to 10.20.30.127.
Use a subnet calculator to double-check your work
Online subnet calculators (search for "subnet calculator" in any browser) let you enter an IP address and prefix length, and they show you the network range, broadcast address, and usable host count. After you calculate your mask by hand, plug in the starting IP and your prefix length to confirm the calculator shows the range you intended.
If the calculator shows a different range than you expected, it usually means the starting IP is not aligned to the subnet boundary. Go back and check whether the host portion of your starting address is all zeros when converted to binary. If it is not, you need a larger subnet mask or a different starting address.
Frequently Asked Questions
What if my address count does not match a power of 2?
Round up to the next power of 2. If you need 100 addresses, use 128 (2 to the 7th power), not 64. You will have unused addresses in the subnet, but the mask will be clean and the math will work. Trying to force a non-power-of-2 count into a subnet creates alignment problems.
Can I use a subnet mask that is larger than I need?
Yes. A larger mask (like /24 instead of /26) gives you more addresses than you need, but it is valid. The trade-off is that you use more address space. In a small network, this usually does not matter. In a large organization with many subnets, oversizing wastes addresses you could assign elsewhere.
What is the difference between prefix length and subnet mask?
/25 and 255.255.255.128 mean the same thing — they both say the first 25 bits are the network. Prefix length is shorter to write and is used in modern network tools. Dotted-decimal is the older format. Most systems accept both.
How do I know if a range spans multiple subnets?
If the ending address is in a different subnet than the starting address (when you apply the mask), your range crosses a subnet boundary. This usually means you need a larger mask. Use a subnet calculator to check: enter the starting IP with your proposed mask, and see if the broadcast address (the last address in that subnet) is greater than or equal to your ending IP.
Why does the starting address have to be aligned?
Subnet masks work by dividing address space into fixed blocks. Each block starts at an address where the host bits are all zeros. If your starting address is not at a block boundary, it is not a valid network address, and routers and network tools will not recognize it as such.