← All lessons
IP addressing and subnetting · Lesson 2 of 7

Masks and CIDR

An IP address has two parts. The first bits say which network it's on; the rest say which host on that network. Every device on the same network shares the network part. Where the split falls is given by the subnet mask.

A subnet mask is 32 bits too: a run of 1s for the network part, then 0s for the host part. 255.255.255.0 is 24 ones then 8 zeros, so in 192.168.1.10 with that mask, 192.168.1 is the network and .10 is the host.

Writing out masks is long, so CIDR notation just counts the 1s after a slash: 192.168.1.10/24 means the mask is 24 ones. The common ones are /8 (255.0.0.0), /16 (255.255.0.0) and /24 (255.255.255.0), but the split can fall anywhere, like /26 or /20.

import ipaddress
net = ipaddress.ip_network("192.168.1.0/24")
print(net.netmask, net.num_addresses)
Prints
255.255.255.0 256

To turn a prefix into a mask, fill whole octets with 255 for every 8 bits, then turn the bits left over into one of the mask octets: /20 is 8 + 8 + 4, so 255.255.240.0 (240 is 11110000). The host part has 32 minus the prefix bits, and so 2 to that power addresses: a /24 has 8 host bits and 256 addresses.

Practice

What subnet mask is /16?

More info

Quick reference

Network partThe first bits: shared by every host on the network.
Host partThe rest: which host it is.
Subnet mask1s for the network, 0s for the host: 255.255.255.0.
CIDR /nThe mask has n ones: /24 = 255.255.255.0.
/8 /16 /24255.0.0.0, 255.255.0.0, 255.255.255.0.
Addresses2^(32 - n) in a /n network.

The other networking courses