Math Tricks for IP Subnetting

William John Holden
2014-01-20

  1. Introduction
  2. Exponential Method
  3. Subtractive Method
  4. Every Bit Doubles
  5. Demonstration
  6. Conclusion

Introduction

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.

Exponential Method

The leftmost (high-ordered) bits indicate the network address, while the rightmost (low-ordered) bits indicate the hosts on that network:

pppppppp.pppppppp.ppppphhh.hhhhhhhh

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.

2^h = 2^n / 2^p

So in the above example, where there are 11 host bits, then the number of possible host addresses in this subnet is

2^11 = 2048

Subtractive Method

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.

Every bit doubles

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.

2^n+1 - 2^n = 2^n+1 / 2 = 2^n

The net mask is given by the sum of the "on" network bits. This can be expressed with summation notation as:

Contains summation notation for 2^i from i=p to n-1

As subtracting descending consecutive powers of two is the same as dividing those powers of two, this expression can be used to give:

Shows summation notation, 2^h = 2^n / 2^p

Demonstration

Given a 14-bit IPv4 prefix, the dotted-decimal subnet is 255.252.0.0, so the number of hosts in this network is:

2^18 = 2^32 / 2^14
2^32 - (2^31 + 2^30 + 2^29 + 2^28 + 2^27 + 2^26 + 2^25 + 2^24 + 2^23 + 2^22 + 2^21 + 2^20 + 2^19 + 2^18)
4294967296 - (2147483648 + 1073741824 + 536870912 + 268435456 + 134217728 + 67108864 + 33554432 + 16777216 + 8388608 + 4194304 + 2097152 + 1048576 + 524288 + 262144)
262144

Using the decimal subtraction method, we use the second octet:

256 - 252 = 4

Seeing the two zeros in 255.252.0.0, multiply the value 4 by 256 twice to find:

4 * 256 * 256 = 262144

Conclusion

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.