0
0
Computer-networksHow-ToBeginner ยท 3 min read

How to Calculate Number of Subnets in Networking

To calculate the number of subnets, use the formula 2^n, where n is the number of bits borrowed from the host portion of the IP address for subnetting. This means you count how many bits you take from the host part to create subnet bits and raise 2 to that power.
๐Ÿ“

Syntax

The formula to calculate the number of subnets is:

Number of Subnets = 2^n

Where:

  • n = Number of bits borrowed from the host portion of the IP address

You borrow bits from the host part of the IP address to create subnet bits, increasing the number of subnets.

python
Number_of_subnets = 2 ** n  # n is the number of borrowed bits
๐Ÿ’ป

Example

Suppose you have a Class C IP address with default subnet mask 255.255.255.0 and you borrow 3 bits for subnetting.

Using the formula, number of subnets = 23 = 8.

This means you can create 8 subnets.

python
def calculate_subnets(borrowed_bits):
    return 2 ** borrowed_bits

borrowed_bits = 3
subnets = calculate_subnets(borrowed_bits)
print(f"Number of subnets when borrowing {borrowed_bits} bits: {subnets}")
Output
Number of subnets when borrowing 3 bits: 8
โš ๏ธ

Common Pitfalls

  • Not counting borrowed bits correctly: Only bits taken from the host portion count, not the network bits.
  • Ignoring reserved subnets: In older standards, the first and last subnets were reserved, but modern networks usually use all subnets.
  • Confusing subnet mask with number of subnets: The subnet mask shows which bits are borrowed, but you must count those bits to calculate subnets.
python
wrong_borrowed_bits = 2  # Incorrect if actually 3 bits borrowed
correct_borrowed_bits = 3

wrong_subnets = 2 ** wrong_borrowed_bits
correct_subnets = 2 ** correct_borrowed_bits

print(f"Wrong subnets calculation: {wrong_subnets}")
print(f"Correct subnets calculation: {correct_subnets}")
Output
Wrong subnets calculation: 4 Correct subnets calculation: 8
๐Ÿ“Š

Quick Reference

Bits Borrowed (n)Number of Subnets (2^n)
12
24
38
416
532
664
7128
โœ…

Key Takeaways

Number of subnets equals 2 raised to the power of borrowed bits from the host portion.
Only count bits borrowed from the host part, not the network part.
Modern networks typically use all subnets without reserving the first and last.
Subnet mask helps identify how many bits are borrowed for subnetting.
Double-check borrowed bits to avoid miscalculating the number of subnets.