How it is calculated
An IPv4 address has 32 bits. The prefix length (CIDR) states how many of them identify the network; the remaining bits number the devices in it.
Addresses in the network = 2^(32 − prefix)
Usable hosts = 2^(32 − prefix) − 2
The first address is the network address, the last one the broadcast address – neither can be given to a device. Exceptions: a /31 network links exactly two devices (RFC 3021), and a /32 stands for a single address.
Example
The address 192.168.178.20 with prefix /24 lies in the network 192.168.178.0/24. 254 addresses from 192.168.178.1 to 192.168.178.254 are usable; 192.168.178.255 is the broadcast address.
Frequently asked questions
Which subnet mask belongs to /24?
255.255.255.0. Each 255 stands for 8 network bits: /16 is 255.255.0.0, /8 is 255.0.0.0.
Why 254 hosts and not 256?
The first address identifies the network itself, and the last is reserved for messages to all devices (broadcast).
Which addresses are private?
10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16. They are not routed on the internet and are meant for home and company networks.
Is there an IPv6 version?
Not yet. IPv6 networks usually have a /64 prefix – giving every network 2⁶⁴ addresses.