X Xerobit

IPv4 Subnetting Tutorial — How to Subnet a Network Step by Step

Subnetting divides a network into smaller subnetworks. Learn how to subnet using CIDR notation: calculate subnets, determine usable hosts, borrow host bits, and subnet a /24...

Mian Ali Khalid · · 6 min read
Use the tool
Subnet / CIDR Calculator
Calculate IPv4 subnets — network, broadcast, usable range, wildcard mask. Input CIDR (/24) or dotted mask (255.255.255.0). Binary visualization.
Open Subnet / CIDR Calculator →

Subnetting is the process of dividing a larger IP network into smaller sub-networks. Understanding how to subnet lets you allocate IP addresses efficiently and design network topologies.

Use the Subnet Calculator to calculate subnet details instantly.

The basics: borrowing bits

To create subnets, you “borrow” bits from the host portion of the address and add them to the network portion:

Original /24 network: 192.168.1.0/24
Network bits: 24
Host bits: 8 (allows 2^8 = 256 addresses, 254 hosts)

Borrow 1 host bit → /25 (2 subnets of 128 addresses each)
Borrow 2 host bits → /26 (4 subnets of 64 addresses each)
Borrow 3 host bits → /27 (8 subnets of 32 addresses each)
Borrow 4 host bits → /28 (16 subnets of 16 addresses each)

Rule: Borrowing n bits creates 2^n subnets, each with 2^(remaining host bits) addresses.

Worked example: subnet 192.168.1.0/24 into 4 subnets

Need 4 subnets: borrow 2 bits → /26

2^2 = 4 subnets ✓
Each subnet: 2^6 = 64 addresses, 62 usable hosts

Subnet 0: 192.168.1.0/26
  Range: .0 – .63
  Network: 192.168.1.0
  First host: 192.168.1.1
  Last host: 192.168.1.62
  Broadcast: 192.168.1.63

Subnet 1: 192.168.1.64/26
  Range: .64 – .127
  First host: 192.168.1.65
  Last host: 192.168.1.126
  Broadcast: 192.168.1.127

Subnet 2: 192.168.1.128/26
  Range: .128 – .191
  First host: 192.168.1.129
  Last host: 192.168.1.190
  Broadcast: 192.168.1.191

Subnet 3: 192.168.1.192/26
  Range: .192 – .255
  First host: 192.168.1.193
  Last host: 192.168.1.254
  Broadcast: 192.168.1.255

Subnetting cheat sheet

PrefixHostsBlock size
/24254
/25126128
/266264
/273032
/281416
/2968
/3024
/312 (no broadcast)2

“Block size” = the increment between subnet network addresses.

VLSM — Variable Length Subnet Masking

VLSM allocates different subnet sizes based on actual needs:

Requirement:
- Marketing: 50 hosts
- Sales: 25 hosts
- IT: 10 hosts
- Management: 5 hosts
- Two WAN links: 2 hosts each

Parent network: 192.168.10.0/24

VLSM allocations (largest first):
1. Marketing (50 hosts) → /26 (62 usable): 192.168.10.0/26
   → .0 to .63

2. Sales (25 hosts) → /27 (30 usable): 192.168.10.64/27
   → .64 to .95

3. IT (10 hosts) → /28 (14 usable): 192.168.10.96/28
   → .96 to .111

4. Management (5 hosts) → /29 (6 usable): 192.168.10.112/29
   → .112 to .119

5. WAN link 1 (2 hosts) → /30: 192.168.10.120/30
   → .120 to .123

6. WAN link 2 (2 hosts) → /30: 192.168.10.124/30
   → .124 to .127

Python: generate all subnets

import ipaddress

def subnet_network(network_cidr: str, new_prefix: int) -> list[dict]:
    """Divide a network into subnets of given prefix length."""
    network = ipaddress.IPv4Network(network_cidr)
    subnets = list(network.subnets(new_prefix=new_prefix))
    
    results = []
    for i, subnet in enumerate(subnets):
        results.append({
            'index': i,
            'network': str(subnet.network_address),
            'cidr': str(subnet),
            'broadcast': str(subnet.broadcast_address),
            'first_host': str(subnet.network_address + 1),
            'last_host': str(subnet.broadcast_address - 1),
            'usable_hosts': subnet.num_addresses - 2,
        })
    
    return results

# Divide 192.168.1.0/24 into /26 subnets:
subnets = subnet_network('192.168.1.0/24', 26)
for s in subnets:
    print(f"{s['cidr']}{s['usable_hosts']} hosts — {s['first_host']} to {s['last_host']}")

Quick mental math

Block size = 256 - last octet of subnet mask

/26 mask = 255.255.255.192 → 256 - 192 = 64 (block size)
/27 mask = 255.255.255.224 → 256 - 224 = 32 (block size)
/28 mask = 255.255.255.240 → 256 - 240 = 16 (block size)
/29 mask = 255.255.255.248 → 256 - 248 = 8  (block size)
/30 mask = 255.255.255.252 → 256 - 252 = 4  (block size)

Subnet addresses start at multiples of the block size:
/26 subnets of 192.168.1.0/24: .0, .64, .128, .192
/27 subnets of 192.168.1.0/24: .0, .32, .64, .96, .128, .160, .192, .224

Related posts

Related tool

Subnet / CIDR Calculator

Calculate IPv4 subnets — network, broadcast, usable range, wildcard mask. Input CIDR (/24) or dotted mask (255.255.255.0). Binary visualization.

Written by Mian Ali Khalid. Part of the Dev Productivity pillar.