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

How to Calculate Subnet: Simple Steps and Examples

To calculate a subnet, convert the subnet mask to binary to find the network and host portions of an IP address. Then, determine the network address by applying a bitwise AND between the IP address and subnet mask, which defines the subnet range and available hosts.
๐Ÿ“

Syntax

Subnet calculation involves these parts:

  • IP Address: The unique address of a device (e.g., 192.168.1.10).
  • Subnet Mask: Defines which part of the IP is the network and which is the host (e.g., 255.255.255.0).
  • Network Address: The result of bitwise AND between IP and subnet mask, identifying the subnet.
  • Broadcast Address: The last address in the subnet, used to send messages to all devices.
  • Host Range: The usable IP addresses between network and broadcast addresses.
text
IP Address: 192.168.1.10
Subnet Mask: 255.255.255.0

Network Address = IP Address AND Subnet Mask
Broadcast Address = Network Address OR Inverted Subnet Mask
Host Range = Addresses between Network and Broadcast
๐Ÿ’ป

Example

This example shows how to calculate the subnet for IP 192.168.1.10 with subnet mask 255.255.255.0.

python
def calculate_subnet(ip, mask):
    def ip_to_bin(ip_str):
        return ''.join(f'{int(octet):08b}' for octet in ip_str.split('.'))

    def bin_to_ip(bin_str):
        return '.'.join(str(int(bin_str[i:i+8], 2)) for i in range(0, 32, 8))

    ip_bin = ip_to_bin(ip)
    mask_bin = ip_to_bin(mask)

    network_bin = ''.join('1' if ip_bin[i] == '1' and mask_bin[i] == '1' else '0' for i in range(32))
    inverted_mask_bin = ''.join('0' if bit == '1' else '1' for bit in mask_bin)

    broadcast_bin = ''.join('1' if network_bin[i] == '1' or inverted_mask_bin[i] == '1' else '0' for i in range(32))

    network_address = bin_to_ip(network_bin)
    broadcast_address = bin_to_ip(broadcast_bin)

    # Host range excludes network and broadcast addresses
    first_host_bin = network_bin[:-8] + '00000001'
    last_host_bin = broadcast_bin[:-8] + '11111110'

    first_host = bin_to_ip(first_host_bin)
    last_host = bin_to_ip(last_host_bin)

    return {
        'Network Address': network_address,
        'Broadcast Address': broadcast_address,
        'Host Range': f'{first_host} - {last_host}'
    }

result = calculate_subnet('192.168.1.10', '255.255.255.0')
for k, v in result.items():
    print(f'{k}: {v}')
Output
Network Address: 192.168.1.0 Broadcast Address: 192.168.1.255 Host Range: 192.168.1.1 - 192.168.1.254
โš ๏ธ

Common Pitfalls

Common mistakes when calculating subnets include:

  • Confusing the subnet mask with the IP address.
  • Not converting IP and mask to binary before calculation.
  • Using the network or broadcast address as a host address.
  • Ignoring that the first and last addresses in a subnet are reserved.
python
Wrong way:
# Using network address as host
host_ip = '192.168.1.0'  # This is network address, not usable

Right way:
host_ip = '192.168.1.1'  # First usable host address
๐Ÿ“Š

Quick Reference

TermDescriptionExample
IP AddressUnique device address192.168.1.10
Subnet MaskDefines network vs host bits255.255.255.0
Network AddressSubnet identifier192.168.1.0
Broadcast AddressSubnet-wide message address192.168.1.255
Host RangeUsable IPs in subnet192.168.1.1 - 192.168.1.254
โœ…

Key Takeaways

Convert IP and subnet mask to binary to identify network and host parts.
Calculate network address by bitwise AND of IP and subnet mask.
Broadcast address is network address OR inverted subnet mask.
First and last addresses in a subnet are reserved and not usable for hosts.
Always verify host IPs fall within the valid host range of the subnet.