Calcylator
Subnet Mask

Subnet mask:
how a /n prefix turns into dotted decimal

Convert a prefix to a mask by hand, count usable addresses and find the network boundary for any IPv4 address.

Calcylator Editorial Team

Updated · 5 min read

What a subnet mask is for

An IPv4 address is a 32-bit number written as four decimal bytes. The subnet mask splits it into two parts: the leading bits that identify the network and the remaining bits that identify a host on that network. A device uses the mask to decide whether a destination is on its own network, in which case it sends directly, or elsewhere, in which case it sends to the gateway.

The mask is written in two ways. Dotted decimal such as 255.255.255.0 is the older form. CIDR notation puts a slash and the number of network bits after the address, so 192.168.1.10/24 carries the same information as the address with mask 255.255.255.0.

Both forms describe a block of ones followed by a block of zeros. A mask like 255.0.255.0 is not valid, because ones must be contiguous from the left.

Private ranges such as 10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16 are reserved for internal networks, and a home or office usually carves a small /24 out of one of them. Public addresses given by an internet provider come with a mask of their own, often much smaller than /24, so the same arithmetic applies at every scale.

Converting a prefix to a mask, one byte at a time

Write n ones followed by 32 − n zeros, split into four groups of eight bits, and convert each group to decimal. A full byte of eight ones is 255, and a partial byte takes one of a few values.

Partial-byte values worth memorising
Ones in the byteBinaryDecimal
110000000128
211000000192
311100000224
411110000240
511111000248
611111100252
711111110254
811111111255
  • Prefix

    /24

  • Ones

    24 = three full bytes

  • Bytes

    11111111.11111111.11111111.00000000

  • Decimal

    255.255.255.0

Subnet mask

255.255.255.0

The last octet is all host bits, giving 2⁸ = 256 addresses.

For a prefix that is not a multiple of eight, such as /26, the last byte holds only two ones, so it is 11000000, which is 192. The mask is 255.255.255.192.

A faster mental shortcut for the interesting byte is to subtract from 256. The mask value of 192 leaves a gap of 64, 224 leaves 32, 240 leaves 16, and 248 leaves 8. That gap is the block size, and it is the number you will keep using when you list subnets or check whether two addresses are neighbours.

Going the other way, from a dotted mask to a prefix, count the one-bits. The mask 255.255.240.0 has 8 + 8 + 4 + 0 = 20 ones, so it is /20. A valid mask can have only one boundary where the ones turn to zeros, which is also how you can spot a typo in a mask someone has typed into a configuration file.

Host counts and the network boundary

Addresses in a subnet =2^(32 − n)
n:
Prefix length
32 − n:
Number of host bits
Subtract 2 for the usable host count in ordinary subnets, because the first address names the network and the last is the broadcast.
PrefixMaskAddressesUsable hosts
/8255.0.0.016,777,21616,777,214
/16255.255.0.065,53665,534
/22255.255.252.01,0241,022
/24255.255.255.0256254
/26255.255.255.1926462
/28255.255.255.2401614
/30255.255.255.25242

Point-to-point links are the exception: a /31 uses both of its two addresses as hosts, and a /32 names a single host. Those are special cases and are not covered by the subtract-two rule.

When you are given a requirement such as "about 500 devices", the reserved-address rule explains why the answer is a /23 rather than a /24. A /24 holds 254 hosts and falls short, whereas a /23 holds 510 and fits with a little room. Reading the table from the bottom upward, each step one bit shorter doubles the usable space, so going from /24 to /23 takes you from 254 to 510 rather than to 508.

The mask also shows how large a broadcast domain can get. Very large subnets are easy to configure but noisy, since every device hears every broadcast, so networks are usually divided into several moderate subnets rather than one enormous block.

Finding the network and broadcast for an address

The network address is the address ANDed with the mask. In decimal, take the byte that contains the boundary, work out the block size, and round the address down to a multiple of it.

  • Host address

    192.168.10.100

  • Prefix

    /26, so mask 255.255.255.192

  • Block size in last octet

    256 − 192 = 64

  • Network

    100 ÷ 64 = 1.56, so block 1 starts at 64

  • Broadcast

    64 + 64 − 1 = 127

Subnet range

192.168.10.64 to 192.168.10.127

Usable hosts are .65 through .126, which is 62 addresses; the gateway is typically one of them.

The same trick works for any prefix. The block size is always 256 minus the interesting octet of the mask, and the network starts at the largest multiple of the block size that does not exceed your address.

Splitting one block into several subnets

Subnetting borrows bits from the host part and gives them to the network part. Every bit you borrow doubles the number of subnets and halves the size of each.

Number of subnets =2^b
b:
Bits borrowed from the host portion
Each subnet then has 2^(host bits left) addresses.
  • Starting block

    192.168.1.0/24

  • Subnets wanted

    4, so borrow 2 bits

  • New prefix

    /26, mask 255.255.255.192

  • Block size

    64 addresses

The four subnets

192.168.1.0, .64, .128 and .192, each /26

Each holds 62 usable hosts; broadcasts are .63, .127, .191 and .255.

This also works backwards for summarising. Four contiguous /26 blocks starting at a multiple of 256 combine into a single /24, which keeps routing tables short.

Choosing the prefix for a number of devices

When the requirement is a count of devices, find the smallest power of two that fits the devices plus the two reserved addresses and any growth.

  • Devices today

    50

  • Growth allowance

    about 20 per cent, so 60

  • Reserved addresses

    2 (network and broadcast)

  • Addresses needed

    62, so the next power of two is 64

  • Host bits

    6, so prefix is 32 − 6 = 26

Prefix chosen

/26 with 62 usable hosts

If the growth case were 70 devices, the next size up is a /25 with 126 usable hosts.

Leave room for infrastructure: gateways, printers, access points and virtual appliances are easy to forget but they all consume addresses.

Mistakes that cause subtle network faults

  • Setting the mask on a host to /24 while the router is /26, so the host believes addresses are local when the router does not.
  • Assigning the network or broadcast address to a device in an ordinary subnet.
  • Choosing a subnet that is too tight, leaving no room for growth, gateways or virtual machines.
  • Confusing a wildcard mask, as used in some router access lists, with a subnet mask. They are bit-for-bit inverses.
  • Forgetting that cloud providers often reserve several additional addresses in every subnet, so the usable count is smaller than the textbook figure.

Common questions

What is the subnet mask for /24?

A /24 prefix has 24 one-bits followed by 8 zero-bits, which is 255.255.255.0 in dotted decimal. It provides 256 addresses and 254 usable hosts in an ordinary subnet, with one address for the network and one for broadcast.

How do I convert CIDR to a subnet mask?

Write n ones followed by 32 − n zeros, split them into four bytes and convert each byte to decimal. For /26, that is 11111111.11111111.11111111.11000000, which is 255.255.255.192.

How many usable hosts are in a /26 subnet?

A /26 has 6 host bits, so 2⁶ = 64 addresses. Subtract the network address and the broadcast address and 62 are usable by devices. The mask is 255.255.255.192, with blocks starting every 64 addresses.

What is the difference between a subnet mask and a wildcard mask?

A wildcard mask is the bitwise inverse of a subnet mask. For 255.255.255.0 the wildcard is 0.0.0.255. Subnet masks mark network bits with ones, while wildcard masks mark the bits that may vary, as in router access lists.

Which subnet does 172.16.0.0/20 cover?

A /20 mask is 255.255.240.0, with a block size of 16 in the third octet. The range runs from 172.16.0.0 to 172.16.15.255, so it holds 4,096 addresses and 4,094 usable hosts.

Was this guide helpful?

Continue reading

View all blogs