William John Holden
Subnetting is among the most difficult subjects to teach in introductory networking classes. Factors that teaching difficult include the transition from decimal to binary (and later hexidecimal), the obscurity of the dotted-decimal subnet mask, and surprising simplicity of the subject. One topic always covered in introductory networking classes is to find the size of a subnet given a prefix or subnet mask. I have seen two techniques, and in this paper I will name them the "exponential method" and the "subtractive method".
Note: IPv4 subnets use the first subnet address, with all host bits zero, for a network identifier and last address, with all host bits one, as a directed broadcast address. This paper ignores this detail because it is not relevant and does not apply to IPv6.
Let n be the total length of your address (32 for IPv4, 128 for IPv6). p is the prefix of high-ordered bits used for addressing networks, thus h, the number of low-ordered bits used for addressing hosts, is given by n = p + h.
The leftmost (high-ordered) bits indicate the network address, while the rightmost (low-ordered) bits indicate the hosts on that network:
In the above address, the first 21 bits in orange represent the network prefix, and the remaining 11 bits in brown indicate the host bits.
To calculate the number of hosts available in the subnet, simply compute two raised to the number of host bits.
So in the above example, where there are 11 host bits, then the number of possible host addresses in this subnet is
The subtraction method is useful when given the dotted-decimal subnet mask instead of a prefix length. Subtract the dotted-decimal octet containing the first zero from 256, then multiply this value by 256 for each remaining octet containing zeros. For example, in a network with a subnet mask of 255.255.248.0, one calculates 256 - 248 = 8 from the first subnet, then multiplies 8 * 256 = 2048 to find an IPv4 subnet with a 25-bit prefix has 2048 host addresses.
Is this shortcut mathematically valid? Yes. Let's explore why, but first, let's look at the "right way" to calculate the number of host addresses in a subnet.
Let's simplify everything by imagining an address system that uses only eight bits and maintains the bit order (most significant bits first). Now let's say you subnet these tiny addresses, again starting from the left, with a mask just like IP where the left bits are all on and right bits are all off. An example netmask might be 240, in binary, 11110000, with gives us 2^4 = 16 available host addresses. Using the dotted decimal method, 256 - 240 also gives us 16. Another way to express 256 - 240 is 2^8 - 240. We get 240 from the bits turned on in binary 11110000, so another way to express 240 is 2^7 + 2^6 + 2^5 + 2^4.
One unique and useful property of binary arithmetic is that subtracting a power of two from the next power of two is the same as diving that next power of two in half.
The net mask is given by the sum of the "on" network bits. This can be expressed with summation notation as:
As subtracting descending consecutive powers of two is the same as dividing those powers of two, this expression can be used to give:
Given a 14-bit IPv4 prefix, the dotted-decimal subnet is 255.252.0.0, so the number of hosts in this network is:
Using the decimal subtraction method, we use the second octet:
Seeing the two zeros in 255.252.0.0, multiply the value 4 by 256 twice to find:
The subtraction method described in this paper quickly calculates the size of a subnet when given the subnet mask in dotted-decimal form. This technique works and is easy to teach. However, I do not recommend teaching this method in introductory networking classes because it may hinder the student in understanding what the bits are doing at the low level. By calculating the size of the subnet as 2^h, the instructor coaxes the student towards thinking in terms of prefix length. The old dotted-decimal subnet mask becomes less and less important as more technologies support CIDR prefix notation and IPv6.
Mathematical expressions shown on this page were rendered by an Online LaTeX Equation Editor from CodeCogs.