Short answer
A subnet mask tells a device which part of an IPv4 address identifies the network and which part identifies the host. 255.255.255.0 means the first 24 bits are the network, the same as /24 in CIDR notation, so 192.168.1.20 with that mask is on network 192.168.1.0 with 256 addresses.
How a subnet mask works
A mask is a 32-bit number written like an IPv4 address. In binary it is always a run of 1s followed by a run of 0s. The 1s mark the network bits, the 0s mark the host bits:
address 192.168.1.20 11000000.10101000.00000001.00010100
mask 255.255.255.0 11111111.11111111.11111111.00000000
network 192.168.1.0 (address AND mask)
A device uses the mask to decide where to send a packet. If the destination has the same network part, it is on the local network and is sent directly; otherwise it goes to the default gateway, normally the router. A wrong mask is a classic cause of "I can reach some devices but not others".
Masks and CIDR notation
The CIDR prefix length is simply the number of 1 bits in the mask. 255.255.255.0 has 24, so it is /24; 255.255.0.0 is /16. CIDR is shorter, and it is what routers, firewalls and cloud consoles mostly use now; IPv6 uses only prefix lengths and has no dotted masks. See CIDR notation explained for the full picture.
Conversion table
| CIDR | Subnet mask | Addresses | Usable hosts |
|---|---|---|---|
| /8 | 255.0.0.0 | 16,777,216 | 16,777,214 |
| /16 | 255.255.0.0 | 65,536 | 65,534 |
| /20 | 255.255.240.0 | 4,096 | 4,094 |
| /21 | 255.255.248.0 | 2,048 | 2,046 |
| /22 | 255.255.252.0 | 1,024 | 1,022 |
| /23 | 255.255.254.0 | 512 | 510 |
| /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 | 2 (point-to-point links) |
| /32 | 255.255.255.255 | 1 | 1 (a single host) |
The only numbers that appear in a valid mask are 0, 128, 192, 224, 240, 248, 252, 254 and 255. Something like 255.255.0.255 is not a valid mask.
Network, broadcast and usable addresses
In a traditional subnet the first address (all host bits 0) names the network and the last (all host bits 1) is the broadcast address, so neither is given to a device. That is why a /24 has 254 usable hosts, not 256. Point-to-point links can use a /31 with both addresses (RFC 3021), and cloud providers often reserve a few more addresses per subnet for their own routers and DNS.
Working one out
Take 10.20.37.99 with mask 255.255.252.0 (/22). The third byte of the mask is 252, leaving blocks of 4 in that byte: 0, 4, 8 … 36, 40. 37 falls in the block starting at 36, so the network is 10.20.36.0/22, running to 10.20.39.255, with 1,022 usable hosts. In Python, ipaddress.ip_interface("10.20.37.99/255.255.252.0").network gives the same answer.
On the public internet you see the same idea in announced routes: every IP lookup on this site shows the prefix that contains the address, such as 8.8.8.0/24 for 8.8.8.8.
[ FAQ ]
Frequently asked questions
What does 255.255.255.0 mean?
It is the subnet mask for a /24 network: the first three numbers of the address identify the network and the last number identifies the device. The network holds 256 addresses, of which 254 can be given to devices.
How do I convert a subnet mask to CIDR?
Count the 1 bits in the mask. Each 255 is 8 bits, and the other values are 128 = 1, 192 = 2, 224 = 3, 240 = 4, 248 = 5, 252 = 6 and 254 = 7. So 255.255.255.192 is 8 + 8 + 8 + 2 = /26.
Does IPv6 use subnet masks?
Not in dotted form. IPv6 always uses a prefix length, such as /64, which plays the same role. Almost every IPv6 LAN is a /64.
What is a wildcard mask?
It is the inverse of a subnet mask, used in some router access lists: 0 bits must match and 1 bits can be anything. The wildcard for 255.255.255.0 is 0.0.0.255.
Last updated · All articles · API docs