What is a subnet mask? Explained with examples
A subnet mask is a 32-bit number that splits an IPv4 address into a network part and a host part: every 1 bit in the mask marks a network bit, every 0 bit a host bit. With the mask 255.255.255.0, the address 192.168.1.20 belongs to network 192.168.1.0 and is host 20 on it. Devices use the mask for one decision, whether another address is on the same network (send directly) or not (send to the default gateway), and the same mask written as a prefix length is the /24 in 192.168.1.0/24.
How 255.255.255.0 works, bit by bit
Each of the four numbers in a mask is one octet of 8 bits. 255 is 11111111 and 0 is 00000000, so 255.255.255.0 is twenty-four 1s followed by eight 0s. The 1s cover the first three octets of an address, which therefore name the network; the 0s cover the last octet, which names the host.
To find the network an address belongs to, keep the address bits wherever the mask has a 1 and set the rest to 0 (a bitwise AND). 192.168.1.20 with 255.255.255.0 gives 192.168.1.0. The last octet has 8 host bits, so the network has 2 to the power of 8 = 256 addresses; the first (.0) is the network address and the last (.255) the broadcast address, leaving 254 for hosts.
How a device uses the mask
When a computer sends a packet, it applies its own mask to its own address and to the destination. If both give the same network, the destination is local and the computer finds it directly with ARP. If not, it sends the packet to its default gateway, the router, which forwards it on.
That is why a wrong mask breaks connectivity in a specific way: a host with a mask that is too long thinks local neighbours are remote and sends their traffic to the router; a mask that is too short makes it try to reach remote addresses directly, and those packets never arrive.
Masks that stop inside an octet
A mask does not have to end on an octet boundary. 255.255.255.224 is twenty-seven 1s: the last octet is 11100000, which is 224. That leaves 5 host bits, so each network has 32 addresses and 30 usable hosts, and the networks start every 32 in the last octet: .0, .32, .64 and so on.
Only nine values can appear in a mask octet: 0, 128, 192, 224, 240, 248, 252, 254 and 255, because the 1 bits must be contiguous from the left. A mask such as 255.255.0.255 is not valid.
Subnet mask and CIDR prefix: two ways to write the same thing
A prefix length counts the 1 bits in the mask. 255.255.255.0 is /24, 255.255.255.224 is /27 and 255.255.0.0 is /16. Routers, cloud consoles and firewall rules mostly use the prefix (10.0.0.0/16); operating systems often show the dotted mask. Converting between them is counting bits.
How to find your subnet mask
Every operating system shows the mask with the address it applies to.
- Windows: run ipconfig in a command prompt and read "Subnet Mask" under your network adapter.
- macOS: run ipconfig getoption en0 subnet_mask in Terminal (en0 is usually Wi-Fi), or read the mask in System Settings > Network > Details > TCP/IP.
- Linux: run ip addr; the mask is shown as a prefix after the address, such as 192.168.1.20/24.
How to choose a mask for a number of hosts
Find the smallest power of 2 that is at least the hosts you need plus 2 (for the network and broadcast addresses). Its exponent is the number of host bits, and 32 minus that is the prefix. For 50 hosts: 52 needs 64, which is 2 to the power of 6, so 6 host bits and a /26, mask 255.255.255.192, with 62 usable hosts.
In AWS and Azure, add 5 instead of 2, because both reserve five addresses in every subnet.
Try it with your own numbers
- Subnet mask chart Every prefix from /8 to /32 with mask, wildcard, block size and usable hosts.
- Subnet calculator Enter any address and mask to get the network, broadcast and host range, with the working shown.
- Subnetting practice questions Unlimited problems with worked answers, so you can do it without a calculator.
Plan your prep
Questions, answered
Source
Exam facts in this guide come from the vendor's published exam guide. Vendors revise these — check the source for the current version before you book.