1 IPv4 Addresses, Subnet Masks, and CIDR
Learn how IPv4 addresses are divided into octets, how subnet masks identify network-prefix bits, and how to convert between CIDR prefix lengths and dotted-decimal masks.
IPv4 address structure
An IPv4 address is bits long and is divided into four groups of bits, called octets. Each is written as a decimal number from to , and periods separate the octets. For example, 192.168.1.70 contains four octets.
Subnet masks and
A separates the bits that belong to the network prefix from the bits available for addresses within that prefix. In a standard CIDR mask, the network bits are contiguous: they are 1 bits from the left, followed only by host bits represented by 0 bits.
writes an address followed by a slash and a number from to . That number is the . For example, 192.168.1.70/26 has prefix bits and remaining host bits. CIDR makes the explicit instead of inferring it from an address’s historical class.
Convert a to a mask
To convert a to a dotted-decimal mask, each complete group of prefix bits becomes an of 255. If the prefix ends partway through an , use the matching partial- value below; all following mask octets are 0.
set bits:
00000000=0set bit:
10000000=128set bits:
11000000=192set bits:
11100000=224set bits:
11110000=240set bits:
11111000=248set bits:
11111100=252set bits:
11111110=254set bits:
11111111=255
For example, /26 fills the first three octets with prefix bits, giving 255.255.255. The remaining prefix bits make the next 11000000, or 192, so /26 is 255.255.255.192.
Convert a mask to a
To convert a dotted-decimal mask to a , count the 1 bits in each and add the counts. For example, 255.255.255.192 contains one-bits, so its is /26.
A valid CIDR must have all its 1 bits together on the left, followed only by 0 bits. 255.255.255.160 is not a valid contiguous CIDR mask: its final is 10100000, which has a 1 after a 0.
Common masks and conversion summary
The following common CIDR prefixes correspond to these dotted-decimal masks:
/8:255.0.0.0/16:255.255.0.0/24:255.255.255.0/25:255.255.255.128/26:255.255.255.192/27:255.255.255.224/28:255.255.255.240/30:255.255.255.252
In short, an IPv4 address has four octets, totaling bits. A distinguishes prefix bits from remaining bits, while CIDR expresses the number of prefix bits as /n. To convert /n to a mask, use 255 for each complete , then the partial- value if needed. To convert a mask to CIDR, count its contiguous leading 1 bits.