Timedora

3 IPv4 Subnetting Practice

Learn to calculate IPv4 subnet boundaries, determine host capacity, choose prefix lengths, and allocate variable-sized subnets.

Address structure and prefixes

An has 3232 bits. In , a prefix such as /26 means that 2626 bits identify the network, leaving 32−26=632-26=6 .

The has the prefix’s number of leading 1 bits followed by 0 bits. For example, the mask for /26 is 255.255.255.192. The prefix determines the number of addresses in the subnet: 232−n2^{32-n}, where nn is the prefix length.

For an ordinary subnet with a prefix from /0 through /30, the typical usable host count is the total address count minus two. The two excluded addresses are the and ; usable host addresses lie between them.

Find subnet boundaries

The can be found by performing a bitwise AND between the IP address and its . Alternatively, use the : find the first mask octet that is neither 255 nor 0, then calculate the block size as 256−mask value256-\text{mask value}. The network begins at a multiple of that block size in the changing octet.

To identify the subnet containing an address, find the block in that octet that includes the address’s value. The start of the block gives the ; the next block’s start is one address after the . Equivalently, the is the last address in the current block. The same method applies when the changing octet is not the last octet.

Apply the

These common prefix sizes show the corresponding masks and typical host capacities:

  • /24: mask 255.255.255.0; 256256 total addresses and 254254 usable hosts.

  • /25: mask 255.255.255.128; 128128 total addresses and 126126 usable hosts.

  • /26: mask 255.255.255.192; 6464 total addresses and 6262 usable hosts.

  • /27: mask 255.255.255.224; 3232 total addresses and 3030 usable hosts.

  • /28: mask 255.255.255.240; 1616 total addresses and 1414 usable hosts.

  • /29: mask 255.255.255.248; 88 total addresses and 66 usable hosts.

  • /30: mask 255.255.255.252; 44 total addresses and 22 usable hosts.

Example: a prefix that changes the last octet

For 192.168.10.77/26, the mask is 255.255.255.192, so the last-octet block size is 256−192=64256-192=64. Blocks begin at .0, .64, .128, and .192. Since .77 falls in the block from .64 through .127, the network is 192.168.10.64 and the broadcast is 192.168.10.127. The is 192.168.10.65–192.168.10.126, containing 6262 addresses.

Example: a prefix that changes the third octet

For 10.23.77.9/20, the mask is 255.255.240.0. The third octet is the changing octet, with a block size of 256−240=16256-240=16. Its blocks begin at 0,16,32,48,64,80,…0,16,32,48,64,80,\ldots; the value 7777 lies in the block from 6464 through 7979. The network is 10.23.64.0, the broadcast is 10.23.79.255, and the usable range is 10.23.64.1–10.23.79.254. There are 212−2=4,0942^{12}-2=4{,}094 typical usable host addresses.

Choose a prefix for a host requirement

To choose a prefix for a required number of hosts, find the smallest number of hh for which 2h−22^h-2 is at least the host requirement. The prefix length is then 32−h32-h.

For a requirement of at least 5050 hosts, h=5h=5 gives 3030 usable addresses, which is insufficient, while h=6h=6 gives 6262, which is enough. The resulting prefix is /26, with 6464 total addresses and 6262 typical usable hosts.

For a requirement of at least 200200 usable hosts, 77 provide 27−2=1262^7-2=126, which is insufficient. 88 provide 28−2=2542^8-2=254, so the smallest suitable prefix is /24, providing 254254 typical usable addresses.

Allocate variable-sized subnets

allows subnets with different sizes to be allocated from one address range. Allocate the largest host requirement first so that smaller requirements can fit into the remaining space. Each allocation must begin on a valid boundary for its block size.

For example, allocate subnets from 192.168.10.0/24 for requirements of 100100, 5050, 2020, and 22 usable hosts:

  • For 100100 hosts, use /25, which has 126126 usable hosts. Allocate 192.168.10.0/25; the usable range is 192.168.10.1–192.168.10.126, and the broadcast is 192.168.10.127.

  • For 5050 hosts, use /26, which has 6262 usable hosts. Allocate 192.168.10.128/26; the usable range is 192.168.10.129–192.168.10.190, and the broadcast is 192.168.10.191.

  • For 2020 hosts, use /27, which has 3030 usable hosts. Allocate 192.168.10.192/27; the usable range is 192.168.10.193–192.168.10.222, and the broadcast is 192.168.10.223.

  • For 22 hosts, use /30, which has 22 usable hosts. Allocate 192.168.10.224/30; the usable range is 192.168.10.225–192.168.10.226, and the broadcast is 192.168.10.227.

These allocations use block sizes of 128128, 6464, 3232, and 44 addresses, respectively.

Practice calculations and answers

Divide a /24 into /27 subnets

A /27 uses blocks of 3232 addresses. A /24 contains 256256 addresses, so it can be divided into 256÷32=8256\div32=8 such subnets. For 192.168.20.0/24, the network addresses are 192.168.20.0, 192.168.20.32, 192.168.20.64, 192.168.20.96, 192.168.20.128, 192.168.20.160, 192.168.20.192, and 192.168.20.224. The first, third, and last are 192.168.20.0, 192.168.20.64, and 192.168.20.224.

Calculate a subnet with a /21 prefix

For 172.16.35.200/21, the mask is 255.255.248.0. The changing octet is the third, with block size 256−248=8256-248=8. The block containing 3535 begins at 3232 and ends at 3939. The network is 172.16.32.0, the broadcast is 172.16.39.255, and the usable range is 172.16.32.1–172.16.39.254.

Exceptions and CIDR context

The usual rule of subtracting two addresses for the usable host count has exceptions. A /31 prefix is used for two-address point-to-point links, and a /32 identifies a single address. A /31 can therefore serve a supported point-to-point link instead of using a conventional /30 subnet when two usable host addresses are required.

CIDR uses prefix lengths rather than historical address classes.