Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 79 ยท The Network Layer: IP

Subnetting and Subnet Masks

Learn subnetting step by step: network and host bits, subnet masks, usable hosts, broadcast addresses and splitting a block into subnets, with a Python calculator.

An organization rarely wants all its computers in one giant network. Putting thousands of hosts in one broadcast domain wastes bandwidth, makes security policy hard and means one mistake affects everybody. Subnetting is the technique of dividing an address block into smaller networks, each with its own range of addresses, by borrowing bits from the host part of the IPv4 address and using them to number the subnets.

Subnetting has a reputation for being difficult, but it only ever uses one idea: an address is 32 bits, the mask says how many leading bits name the network, and everything else is arithmetic on powers of two. In this lesson you will learn to read a mask, compute the network, broadcast and host range of any address, and split a block into equal subnets. A Python calculator lets you check your own working.

What the subnet mask does

A subnet mask is a 32-bit number made of a run of 1 bits followed by a run of 0 bits. Wherever the mask has a 1, the corresponding address bit belongs to the network part; wherever it has a 0, the bit belongs to the host part.

Text
address   192.168.10.77   11000000.10101000.00001010.01001101
mask      255.255.255.0   11111111.11111111.11111111.00000000
network   192.168.10.0    11000000.10101000.00001010.00000000

The network address is the bitwise AND of the address and the mask. A host does this calculation to decide whether a destination is on its own network (deliver directly) or elsewhere (send to the default gateway): it ANDs both addresses with its own mask and compares the results.

Masks are written in two ways that mean the same thing:

Dotted mask Prefix length Host bits Addresses
255.0.0.0 /8 24 16,777,216
255.255.0.0 /16 16 65,536
255.255.255.0 /24 8 256
255.255.255.128 /25 7 128
255.255.255.192 /26 6 64
255.255.255.224 /27 5 32
255.255.255.240 /28 4 16
255.255.255.248 /29 3 8
255.255.255.252 /30 2 4

The prefix length (/24) counts the leading ones, and the notation 192.168.10.77/24 combines address and mask in one string. Valid masks are contiguous: 255.255.255.160 (binary ends in 10100000) is not a legal mask.

The last octet of a mask can only take the values 0, 128, 192, 224, 240, 248, 252, 254 and 255, because each is a run of ones followed by zeros. It helps to memorise them.

Computing everything about a subnet

Given an address and a prefix length of n, there are five numbers you can compute. Let h = 32 - n be the number of host bits.

  1. Network address: address AND mask (host bits all 0).
  2. Broadcast address: network address with all host bits set to 1, equal to the network address plus 2^h - 1.
  3. First usable host: network address + 1.
  4. Last usable host: broadcast address - 1.
  5. Number of usable hosts: 2^h - 2.

A worked example: 192.168.10.77/26. The prefix is 26 so the host bits are 6, and each subnet has 64 addresses. The last octet 77 falls in the block that starts at the largest multiple of 64 not above 77, which is 64. So:

  • Network: 192.168.10.64
  • Broadcast: 192.168.10.127 (64 + 63)
  • Usable range: 192.168.10.65 to 192.168.10.126
  • Usable hosts: 2^6 - 2 = 62

That "block size" shortcut is the quickest way to work by hand: the block size is 256 minus the interesting octet of the mask (here 256 - 192 = 64), and subnets begin at multiples of the block size.

Two special cases

The "minus 2" rule has exceptions for tiny networks. A /31 has only two addresses and, under RFC 3021, both are usable, which is meant for point-to-point links between routers, saving addresses. A /32 is a single host address, used for a router's loopback interface and for host routes. The /30, with 4 addresses and 2 usable hosts, is the traditional point-to-point choice.

Wildcard masks

Some router configuration languages (access lists, OSPF) use the inverse of the subnet mask, called a wildcard mask: ones where the address may vary. The wildcard for 255.255.255.192 is 0.0.0.63.

Python Subnet calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Same network or not?

Two hosts can talk directly only if they are on the same network, meaning the AND of each address with the mask gives the same result. This is the check a host performs before every packet it sends. Suppose host A is 192.168.1.200/25 and host B is 192.168.1.20/25. A mask of /25 splits the last octet at 128: A is in 192.168.1.128/25, B in 192.168.1.0/25. They are on different networks, even though the first three octets match, so A must send traffic for B through a router.

This is a common cause of misconfiguration: if one host has the mask 255.255.255.0 and its neighbor 255.255.255.128, they disagree about who is local, and traffic works in one direction but not the other.

Splitting a block into subnets

Subnetting a block means extending the prefix by k bits, which yields 2^k equal subnets, each with 2^k times fewer addresses. The new bits are the "subnet number".

Take 192.168.10.0/24 and split it into four subnets. Four is 2^2 so k = 2 and the new prefix is /26. Each subnet has 64 addresses:

flowchart TB
    B["192.168.10.0/24<br/>256 addresses"]
    B --> S0["192.168.10.0/26<br/>hosts .1 to .62"]
    B --> S1["192.168.10.64/26<br/>hosts .65 to .126"]
    B --> S2["192.168.10.128/26<br/>hosts .129 to .190"]
    B --> S3["192.168.10.192/26<br/>hosts .193 to .254"]

Borrowing two host bits creates four /26 subnets, each with 62 usable hosts. In total you lose 4 x 2 = 8 addresses to network and broadcast overhead, compared with only 2 for the single /24.

To plan a split you need two numbers, and they pull in opposite directions:

  • Number of subnets needed: choose the smallest k with 2^k at least that many.
  • Hosts per subnet needed: choose the smallest host-bit count h with 2^h - 2 at least that many.

Both must fit together in the original block: the prefix of the block + k + h can be at most 32. If you need 6 subnets of 25 hosts each out of a /24: k = 3 (8 subnets), h = 5 (30 hosts), and 24 + 3 + 5 = 32, which fits exactly.

When subnets need different sizes, equal splitting wastes addresses; use the flexible method in CIDR and VLSM.

Python Split a block and find which subnet a host is in

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why subnet? Benefits and drawbacks

Without subnetting, all hosts of a class A, B or C network sit on one broadcast domain: one broadcast reaches everybody, and a problem anywhere affects everyone. With subnetting you split the block into smaller networks joined by routers. The benefits:

  • Less broadcast traffic. Each subnet is its own broadcast domain.
  • Better performance and easier management. Traffic between hosts of one subnet stays local, and a department's problems stay in its subnet.
  • Improved security. You can place firewall rules and access lists between subnets, for example between staff, servers and guests.
  • Traffic prioritization. Quality-of-service rules can treat the voice subnet differently from the guest subnet.
  • Efficient use of addresses and scalability. The network is organized by department or floor, new subnets can be added as the organization grows, and routing tables can still show the whole organization as one summarized route.

The drawbacks are that planning takes effort, a router (or layer-3 switch) is needed to move traffic between subnets, some addresses are lost in every subnet (network and broadcast), and old equipment or routing protocols that do not carry the subnet mask (classful ones) cannot cope with it.

Hosts and subnets for any mask

For a mask with h host bits and a block that you divide using s borrowed bits:

  • usable hosts per subnet = 2^h - 2 (the all-zero host part is the network address and the all-one host part is the broadcast address),
  • number of subnets = 2^s.

To adjust the mask, borrow bits from the host part: each borrowed bit doubles the number of subnets and roughly halves the hosts per subnet. To calculate, convert the mask to binary, count the host bits (the zeros), and apply the formulas. The program shows the pattern for a /24 block.

Python Subnets and hosts for each borrowed bit

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A method you can do on paper

With practice you can subnet in your head using this routine:

  1. Write the prefix length. Find the interesting octet, the one where the mask is neither 255 nor 0.
  2. Compute the block size: 256 minus the mask's value in that octet.
  3. The network address is the largest multiple of the block size not above the host's value in that octet (with lower octets set to 0).
  4. The broadcast address is the next network minus 1.
  5. The host range lies between the two, excluding both ends.

Check with 172.16.77.130/21. The mask is 255.255.248.0, the interesting octet is the third (248), block size 8. The third octet 77 lies in the block starting at 72. So the network is 172.16.72.0, the next network is 172.16.80.0, the broadcast is 172.16.79.255, and the hosts run from 172.16.72.1 to 172.16.79.254.

When a question gives a number of hosts, remember to add 2 (network and broadcast) before looking for the next power of two. A department with 50 hosts needs 52 addresses, so a block of 64 (/26), not 32.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the network address of 172.16.77.130/21?

    /21 gives mask 255.255.248.0 and a block size of 8 in the third octet. 77 lies in the block that starts at 72, so the network is 172.16.72.0.
  2. 2. How many usable host addresses does a /27 network have?

    A /27 has 5 host bits, so 32 addresses; subtracting the network and broadcast addresses leaves 30.
  3. 3. Which of these is a valid subnet mask?

    A mask must be a run of 1 bits followed by 0 bits. 255.255.240.0 is /20; the others have 1s after 0s.
  4. 4. You need at least 5 subnets, each holding 25 hosts, from one block. What prefix length per subnet is the best fit when starting from a /24?

    25 hosts need 5 host bits (30 usable), giving /27. A /24 divides into 8 such subnets, so 5 are available.
  5. 5. Host A is 10.0.0.200/25 and host B is 10.0.0.30/25. Are they on the same network?

    A /25 splits the last octet at 128. 200 is in the upper half and 30 in the lower half, so they need a router to talk.

Summary

  • A subnet mask marks the network bits with ones and the host bits with zeros. The network address is the address ANDed with the mask.
  • Each subnet with h host bits has 2^h addresses and 2^h - 2 usable hosts. The first address is the network, the last is the broadcast. /31 (point-to-point) and /32 (single host) are special.
  • To split a block, extend the prefix by k bits to get 2^k subnets. Check that the subnet count and host count both fit.
  • By hand: find the interesting octet, compute block size = 256 - mask value, and count in multiples of it.
  • Hosts compare masked addresses to decide whether a destination is local or must go through the gateway.

Next, see how to give subnets different sizes and combine many into one route in CIDR, VLSM and route aggregation.