Calculating the number of hosts in a subnet is a fundamental networking skill. It primarily depends on the subnet mask, which defines how many bits are allocated for the network portion and how many for the host portion of an IP address.
Here's a breakdown of how to do it:
The Core Concept: Host Bits
An IPv4 address is 32 bits long. The subnet mask divides these 32 bits into two parts:
- Network bits (1s in the subnet mask): These bits identify the network.
- Host bits (0s in the subnet mask): These bits identify individual hosts within that network.
The more host bits you have, the more possible host addresses are available.
The Formula
The number of total possible addresses within a subnet is calculated as:
2^H
Where H is the number of host bits.
However, you can't use all of these addresses for actual hosts because two addresses are reserved:
- Network Address: The first address in the subnet, where all host bits are
0. This identifies the subnet itself.
- Broadcast Address: The last address in the subnet, where all host bits are
1. This is used to send data to all devices on that subnet.
So, the number of usable hosts in a subnet is:
2^H - 2
How to Find 'H' (The Number of Host Bits)
You typically encounter subnet masks in two formats:
Method 1: Using CIDR Notation (e.g., /24, /26)
CIDR (Classless Inter-Domain Routing) notation is the most common and easiest way. The number after the slash (/) indicates how many bits are used for the network portion.
- Total IPv4 bits: Always
32.
- Network bits: The number given in the CIDR notation (e.g.,
N for /N).
- Host bits (H):
32 - Network bits
Example: A /24 subnet mask
- Total bits = 32
- Network bits = 24
- Host bits (H) = 32 - 24 = 8
Method 2: Using Dotted-Decimal Subnet Mask (e.g., 255.255.255.0)
If you have the subnet mask in dotted-decimal format, you'll need to convert it to binary (or recognize common patterns).
- Convert each octet of the subnet mask to binary.
- Count the number of '0's. These are your host bits.
Example: Subnet mask 255.255.255.0
255 in binary is 11111111 (8 ones)
0 in binary is 00000000 (8 zeros)
So, the subnet mask in binary is:
11111111.11111111.11111111.00000000
- Count the '0's: There are 8 zeros in the last octet.
- Host bits (H) = 8
Step-by-Step Calculation Examples
Let's put it all together:
Example 1: 192.168.1.0/24
-
Find H (Host Bits):
- CIDR is
/24.
- H = 32 - 24 = 8 host bits.
-
Calculate Total Addresses:
2^H = 2^8 = 256 addresses.
-
Calculate Usable Hosts:
-
2^H - 2 = 256 - 2 = 254 usable hosts.
-
(The network address would be 192.168.1.0, and the broadcast address would be 192.168.1.255).
Example 2: 10.0.0.0/26
-
Find H (Host Bits):
- CIDR is
/26.
- H = 32 - 26 = 6 host bits.
-
Calculate Total Addresses:
2^H = 2^6 = 64 addresses.
-
Calculate Usable Hosts:
2^H - 2 = 64 - 2 = 62 usable hosts.
Example 3: 172.16.50.0 with subnet mask 255.255.255.252
-
Find H (Host Bits):
- Convert
255.255.255.252 to binary:
255 = 11111111
255 = 11111111
255 = 11111111
252 = 11111100 (128+64+32+16+8+4 = 252)
- Full binary mask:
11111111.11111111.11111111.11111100
- Count the '0's: There are 2 zeros in the last octet.
- H = 2 host bits. (This corresponds to a
/30 CIDR mask).
-
Calculate Total Addresses:
-
Calculate Usable Hosts:
Summary Steps:
- Determine the number of host bits (H):
- If using CIDR
/N, then H = 32 - N.
- If using dotted-decimal mask, convert to binary and count the
0s.
- Calculate total addresses:
2^H.
- Calculate usable hosts:
2^H - 2.
Understanding this calculation is key to effective IP addressing and subnetting.