How to Calculate a Subnet Mask and Host Count from CIDR

From an IPv4 address and a prefix length such as /24, gives the subnet mask, the network and broadcast addresses and the number of usable hosts, which excludes the first and last addresses.

From an IPv4 address and a prefix length, this tool gives the subnet mask, the network and broadcast addresses and the number of usable hosts.

N=232pN = 2^{32 - p}
H=N2H = N - 2

Here pp is the prefix length, NN the total addresses in the block and HH the usable hosts.

What the prefix length means

An IPv4 address is 32 bits. The prefix length says how many of the leading bits identify the network. With /24 the first 24 bits are the network and the remaining 8 belong to the hosts.

The subnet mask is those leading bits set to one, written in the same dotted form as an address. For /24 that is 255.255.255.0.

Why two addresses are subtracted

The first address in a block names the network itself and the last is the broadcast address. Neither can be assigned to a machine, so the usable host count is the block size less two.

Worked example

Taking 192.168.10.130 with a /26 prefix gives a mask of 255.255.255.192, a network address of 192.168.10.128, a broadcast address of 192.168.10.191 and 62 usable hosts.

A /26 leaves six host bits, so blocks are 64 addresses wide. Dividing the last octet, 130, by 64 gives two remainder two, so the address sits in the block starting at 128. The block ends at 128 + 64 − 1 = 191.

Points to watch

The prefixes /31 and /32 are counted differently. A /31 carries a point-to-point link and offers both of its two addresses; a /32 names a single host. Neither subtracts the usual two.