To subnet a network into smaller subnets we “borrow” host bits and add them to the network portion of the address. For example, say we bought a Class C IP address range, such as 200.15.10.0/24. This gives us:

  1. 1 network
  2. 254 hosts

What we can do with it is move the line that separates the network portion to the right, taking bits from the host portion. The more we do that, the more subnets we will create, but each subnet will have fewer hosts.

Given an IP/mask

Calculate number of subnets and hosts given a network and mask

Given 60.0.0.0/8, and a subnet mask 255.255.255.240 calculate:

  1. How many subnets?
  2. How many hosts per subnet?

Given the mask ending with 240, binary is 11110000 (or /28). Thus the binary address is:

11111111.11111111.11111111.1111000

Because this is a Class A, /8 subnet, the “borrowed” bits are 16 (from the middle octets) + 4 (from the last octet) = 20. Thus, the number of subnets is 220 = 1,048,576. The number of hosts is based on the zeros left in the mask; thus, 24 - 2 = 14 hosts per network.

Given an IP address, find net, broadcast, and range

Given 60.15.10.75/28, calculate:

  1. Network address
  2. Broadcast address
  3. Valid IP range

The subnet mask is /28, which converts to:

11111111.11111111.11111111.11110000

Additionally, the full IP address is:

00111100.00001111.00001010.01001011

From looking at the subnet mask, we know the network portion of the address ends with the 4th digit of the last octet. Now, if we only look at the first 4 digits of the last octet of the actual IP address (0100) we know that this value is 64. Thus, the network address is 60.15.10.64.

We also know that the IP range is 16 addresses long by comparing the subnet mask and taking the last number prior to the network/host boundary:

|128| 64| 32| 16|  8|  4|  2|  1|
|  0|  1|  0|  0|  1|  0|  1|  1| # Last IP octet
|  1|  1|  1|  1|  0|  0|  0|  0| # Subnet Mask
				|                 # Boundary

Since the host IP range is 16 numbers, and we know the network address is 64, we know that the next network address is at 60.15.10.80. This means that the broadcast address is at 79.

Finally, with the network and broadcast address known, we know the valid host IP range is 65 to 78.

Subnet magic number method

Step-by-Step Algorithm Using the “Magic Number” Method

  1. Identify the subnet mask

    • Convert the CIDR prefix (e.g., /21) to a dotted decimal subnet mask.
  2. Find the “Magic Number”

    • Subtract the interesting octet from 256:
      • Magic Number = 256 − Subnet Mask in the interesting octet
    • The “interesting octet” is the first non-255 octet in the subnet mask.
  3. Determine the Network Address

    • Find the largest multiple of the magic number less than or equal to the given IP’s value in the interesting octet.
    • Keep other octets unchanged.
  4. Determine the Broadcast Address

    • Add (Magic Number - 1) to the network address in the interesting octet.
    • Change all host bits (remaining octets) to 255.
  5. Find the Valid IP Range

    • First usable IP = Network Address +1
    • Last usable IP = Broadcast Address -1

Example: Find the subnet details for 172.23.16.0/21

Step 1: Identify the subnet mask

  • CIDR: /21
  • Subnet mask: 255.255.248.0
    • The interesting octet is 248 (third octet).

Step 2: Find the Magic Number

  • Magic Number = 256 - 248 = 8
  • This means subnet blocks are in multiples of 8 in the third octet.

Step 3: Find the Network Address

  • Look at the third octet in 172.23.16.0.
  • The largest multiple of 8 that is ≤ 16 is 16.
  • So, the network address is:
    172.23.16.0

Step 4: Find the Broadcast Address

  • Add (Magic Number - 1) = 8 - 1 = 7 to the third octet of the network address: 16+7=23
  • The broadcast address is:
    172.23.23.255 (last octet is 255 because all host bits are set to 1)

Step 5: Find the Valid IP Range

  • First Usable IP: 172.23.16.1 (Network Address +1)
  • Last Usable IP: 172.23.23.254 (Broadcast Address -1)

Final Answer

ParameterValue
Network Address172.23.16.0
Broadcast Address172.23.23.255
First Usable IP172.23.16.1
Last Usable IP172.23.23.254

Summary of the Magic Number Process

  1. Magic Number = 256 - subnet mask in the interesting octet
  2. Network Address:
    • Largest multiple of the Magic Number ≤ given IP in the interesting octet.
  3. Broadcast Address:
    • Network address + (Magic Number - 1), with all host bits set to 1.
  4. Valid Range:
    • First Usable IP = Network Address +1
    • Last Usable IP = Broadcast Address -1
Link to original

Calculating the number of networks

Available networks = 2 ^ subnet-bits

Note

To calculate this by hand, simply count with fingers and double (starting with 2) for as many subnet-bits we have, i.e. 24 can be done via the following sequence: 2, 4, 8, 16.

To calculate the number of available subnets, use the formula 2subnet-bits. For example, if we are using a Class C network with a /28 subnet mask, we have “borrowed” 4 bits from the default /24. As a result, we have 24 = 16 available subnets.

Likewise, if we have a Class B network with a /28 subnet mask, we have borrowed 12 bits from the /16 default. Thus, we have 212 = 4096 available subnets.

By doing this, we will require routers to communicate between hosts on different subnets.

Calculating the number of hosts

Available hosts = (2 ^ host-bits) - 2

To calculate the number of available hosts in a subnet, use the formula 2host-bits - 2. For example, in a Class C network with a /28 subnet mask we have 4 bits left for hosts (32 - 28). Thus, there are 14 hosts available on this subnet.

Note

We subtract 2 from calculating the number of hosts available because we need to remove the network address and the broadcast address.

Likewise, a Class B network with a subnet mask of /28 will also give us 14 hosts.

Note

The /31 subnet breaks the standard rules of IP addressing because it does not leave space for the network and the broadcast address. Cisco routers support this for “point to point” links.

To see the number of valid host addresses goes up based on the number just prior to the line that separates network address from host address (assuming, here, that we subnet using /30):

/31 vs /30 subnets

They both allow a maximum of 2 hosts, and thus are used for point to point networking.

Note

Although /31 is valid, do not use it on the CCNA unless explicitly told to.

Subnetting large networks