Subnetting: Masks, Prefix Lengths, VLSM and Broadcast Addresses
How a subnet mask splits an address into network and host parts, how to find the network, broadcast and usable range of any subnet by hand, how to plan subnets of different sizes with VLSM, and why /31 links and directed broadcasts behave as they do.
kvi5hnuCode Sprout team member16 min readOn this page
Classful addressing (article 7) gave each organization one big network. But an organization rarely has one big network. It has offices, floors, labs, server rooms and Wi-Fi, each a separate LAN, and the links between its routers. Asking a central registry for a new network number every time someone added a LAN didn't scale.
Subnetting, standardized in 1985, solved this. An organization takes the block it was given and divides it locally into subnets, without the rest of the Internet having to know. This article teaches the arithmetic. It's the most practical skill in IP addressing: you'll use it to configure hosts, read routing tables, write firewall rules and debug "why can't these two machines talk?".
Borrowing host bits
The trick is to move the line between the network and host parts. Suppose an organization was given 198.51.100.0/24: 24 network bits fixed by the registry and 8 host bits for local use. It can take some of those 8 host bits and use them as a subnet number:
- Use 2 host bits as the subnet number, and the organization gets 2² = 4 subnets, each with 6 host bits, so 64 addresses.
- Use 3 bits and it gets 8 subnets of 32 addresses.
Nothing outside the organization changes. The rest of the Internet still routes all of 198.51.100.0/24 to the organization's border router. Only the routers and hosts inside need to know where the subnet boundaries are, and they learn it from the subnet mask.
Subnet masks and prefix lengths
A subnet mask is a 32-bit value with a 1 in every position that belongs to the network part (including the subnet bits) and a 0 in every host position. It's written like an address, as a dotted quad. Because the 1 bits must all come first, followed by all the 0 bits, a mask can be described by just counting its 1 bits. That count is the prefix length, written after a slash: /26 means the mask has 26 one bits.
Here are common masks, computed in the lab, with how many addresses each subnet contains (2 to the power of the number of host bits):
prefix mask mask in binary addresses
/8 255.0.0.0 11111111 00000000 00000000 00000000 16777216
/16 255.255.0.0 11111111 11111111 00000000 00000000 65536
/20 255.255.240.0 11111111 11111111 11110000 00000000 4096
/23 255.255.254.0 11111111 11111111 11111110 00000000 512
/24 255.255.255.0 11111111 11111111 11111111 00000000 256
/25 255.255.255.128 11111111 11111111 11111111 10000000 128
/26 255.255.255.192 11111111 11111111 11111111 11000000 64
/27 255.255.255.224 11111111 11111111 11111111 11100000 32
/30 255.255.255.252 11111111 11111111 11111111 11111100 4
/31 255.255.255.254 11111111 11111111 11111111 11111110 2
/32 255.255.255.255 11111111 11111111 11111111 11111111 1
Two things to memorize: the values a mask byte can take, which are only 0, 128, 192, 224, 240, 248, 252, 254 and 255, and that every bit you add to the prefix halves the subnet.
Network, broadcast and host addresses
Given any address and its prefix length, you can work out its whole subnet with two bitwise operations:
- Network address: address AND mask. Every host bit becomes 0. This is the subnet's name, used in routing tables.
- Broadcast address: address OR (NOT mask). Every host bit becomes 1. A packet to this address reaches every host on the subnet.
- Usable host addresses: everything in between.
AND gives a 1 only where both bits are 1, OR gives a 1 where either bit is 1, and NOT flips every bit. Take 198.51.100.77/26. The mask is 255.255.255.192, so the first three bytes are all network bits and pass through unchanged. Only the last byte needs work:
address byte 77 = 01001101
mask byte 192 = 11000000
AND = 01000000 = 64 network address 198.51.100.64
NOT mask = 00111111
address OR NOT = 01111111 = 127 broadcast address 198.51.100.127
The diagram shows the same calculation across all 32 bits:

The lab computes the same thing with Python's ipaddress module:
address 198.51.100.77
prefix 26 -> 255.255.255.192
network 198.51.100.64
broadcast 198.51.100.127
first host 198.51.100.65
last host 198.51.100.126
usable hosts 62
The fast way, without binary
You rarely need to write out 32 bits. Use the block size instead:
- Find the byte where the mask isn't 255 or 0. For /26 it's the last byte, 192.
- Block size = 256 − that byte. 256 − 192 = 64.
- Subnets in that byte start at multiples of the block size: 0, 64, 128, 192.
- The address's byte (77) falls in the block starting at 64. So the network is .64, the next block starts at .128, and the broadcast is one less: .127.
This works in any byte. For 10.20.200.9/20, the interesting byte is the third (240), block size 256 − 240 = 16, and the third byte of the address is 200. 200 ÷ 16 = 12.5, so it falls in the block that starts at 12 × 16 = 192 and ends just before 13 × 16 = 208, that is 192–207. So the network is 10.20.192.0/20 and the broadcast is 10.20.207.255.
Who's on my subnet?
This calculation is what a host does before sending every packet. If the destination is inside its own subnet, it sends the packet directly on the local link. If not, it sends it to its router (the default gateway). Here's the decision for a host at 198.51.100.77/26:
198.51.100.100 on my subnet: send directly
198.51.100.127 on my subnet: send directly
198.51.100.128 not on my subnet: send to the router
198.51.100.30 not on my subnet: send to the router
How Linux stores a subnet
When you give a Linux interface an address with a prefix length, the kernel works out the rest. One caveat: ip addr add doesn't fill in the broadcast address unless you ask for it with brd +. Here's the lab adding 198.51.100.77/26 to server twice, first without brd + and then with it. Each pair of lines is the ip addr listing after one attempt, and only the second pair shows brd 198.51.100.127:
inet 198.51.100.10/24 scope global eth0
inet 198.51.100.77/26 scope global eth0
inet 198.51.100.10/24 scope global eth0
inet 198.51.100.77/26 brd 198.51.100.127 scope global eth0
The kernel also adds entries to its local routing table, which lists the addresses this host owns (local) and the broadcast addresses it accepts (broadcast). This listing was taken after the second ip addr add, the one with brd +, so 198.51.100.127 is there for the /26. Note also the entry for 198.51.100.255: the original 198.51.100.10/24 was added without brd, and its ip addr line shows none, but the kernel still accepts that subnet's broadcast address. (scope host on the local lines means those entries are only used inside this host.)
local 198.51.100.10 dev eth0 proto kernel scope host src 198.51.100.10
local 198.51.100.77 dev eth0 proto kernel scope host src 198.51.100.77
broadcast 198.51.100.127 dev eth0 proto kernel scope link src 198.51.100.77
broadcast 198.51.100.255 dev eth0 proto kernel scope link src 198.51.100.10
Variable-length subnet masks (VLSM)
The first subnetting rules expected a whole organization to use one mask everywhere, so every subnet had the same size. Real networks don't fit that: a 100-person office LAN, a 10-server rack and a link between two routers need very different numbers of addresses.
Variable-length subnet masks (VLSM) allow different prefix lengths in different parts of the same block. Every modern routing protocol (the protocols routers use to tell each other about networks, such as OSPF, IS-IS, RIPv2 and BGP) carries a prefix length with each route, so VLSM just works. Only the very old RIP version 1, which never sent masks, can't handle it.
Here's a plan for 198.51.100.0/24 computed in the lab. Each subnet gets the smallest prefix that fits its hosts plus the network and broadcast addresses, and the largest subnets are placed first:
subnet need prefix usable range
Office LAN 100 198.51.100.0/25 126 198.51.100.1 - 198.51.100.126
Lab LAN 50 198.51.100.128/26 62 198.51.100.129 - 198.51.100.190
Wi-Fi guests 25 198.51.100.192/27 30 198.51.100.193 - 198.51.100.222
Servers 10 198.51.100.224/28 14 198.51.100.225 - 198.51.100.238
Router link 1 2 198.51.100.240/31 2 198.51.100.240 - 198.51.100.241
Router link 2 2 198.51.100.242/31 2 198.51.100.242 - 198.51.100.243
left over: 198.51.100.244/30, 198.51.100.248/29

The rules that make a plan like this work:
- Every subnet starts on a multiple of its own size. A /26 (64 addresses) can start at .0, .64, .128 or .192, never at .100. That's what keeps the network and broadcast calculations above correct.
- Allocate the biggest subnets first. Placing small subnets first fragments the space, leaving gaps that no big subnet fits into.
- Leave room to grow. The plan above has .244/30 and .248/29 free. A real plan usually also leaves larger gaps next to subnets likely to grow.
Point-to-point links: /30 and /31
A link between two routers needs exactly two addresses. With the usual rule (lose the network and broadcast addresses), the smallest usable subnet is a /30: 4 addresses, 2 usable, half wasted. Since 2000, /31 prefixes are allowed on point-to-point links: both addresses are usable, and there's no broadcast address, because on a link with only two ends there's nobody else to broadcast to. For IPv6 the equivalent recommendation is a /127.
Here's a /31 link in the lab between two hosts, p1 (203.0.113.0) and p2 (203.0.113.1). Note the addresses end in .0 and .1, which would be the "network" and "broadcast" addresses of a /31 under the old rule, and both work:
inet 203.0.113.0/31 scope global eth0
inet 203.0.113.1/31 scope global eth0
64 bytes from 203.0.113.1: icmp_seq=1 ttl=64 time=0.785 ms
64 bytes from 203.0.113.1: icmp_seq=2 ttl=64 time=0.182 ms
Broadcast addresses
IPv4 has two kinds of broadcast address:
- Subnet broadcast (also called a directed broadcast): the subnet's address with every host bit set to 1, such as 198.51.100.255 for 198.51.100.0/24. It means "every host on that subnet".
- Limited broadcast: 255.255.255.255. It means "every host on the link I'm attached to", and routers never forward it. A host that doesn't know its own address yet, such as a DHCP client at boot, uses it.
On the local link, both become a link-layer broadcast frame that every interface receives (article 14). The interesting behavior is in how hosts and routers react.
Hosts ignore broadcast pings by default
Linux, like most systems, doesn't answer ICMP echo requests (pings) sent to a broadcast address, because doing so would let one packet trigger a reply from every host on a network. In the lab, the router r1 pings the server LAN's broadcast address, 198.51.100.255, and nobody answers. The setting printed first, icmp_echo_ignore_broadcasts = 1, is the protection that makes hosts stay silent (the lab shows it on server), and the WARNING line is just ping noting that the target is a broadcast address:
net.ipv4.icmp_echo_ignore_broadcasts = 1
WARNING: pinging broadcast address
2 packets transmitted, 0 received, 100% packet loss, time 1065ms
After turning that protection off on the two LAN hosts (server at .10 and dns at .53), the same ping gets a reply from each of them. ping reports the second reply to the same request as a duplicate (+1 duplicates in the summary):
WARNING: pinging broadcast address
PING 198.51.100.255 (198.51.100.255) 56(84) bytes of data.
64 bytes from 198.51.100.53: icmp_seq=1 ttl=64 time=0.758 ms
64 bytes from 198.51.100.10: icmp_seq=1 ttl=64 time=0.768 ms
64 bytes from 198.51.100.53: icmp_seq=2 ttl=64 time=0.299 ms
--- 198.51.100.255 ping statistics ---
2 packets transmitted, 2 received, +1 duplicates, 0% packet loss, time 1062ms
Routers don't forward directed broadcasts
In principle, a directed broadcast could cross the Internet as one packet and become a broadcast only when it reached the target subnet. The router requirements standard of 1995 even said routers should forward them by default. That turned out to be a gift to attackers: the smurf attack sent pings to a remote subnet's broadcast address with the victim's address forged as the source, so every host on that subnet replied to the victim at once. Since 1999 routers must not forward directed broadcasts by default.
Linux follows this. Here client, on the other side of r1, pings 198.51.100.255. With forwarding of directed broadcasts off (the default, shown by the router's bc_forwarding = 0 settings), nothing comes back:
net.ipv4.conf.all.bc_forwarding = 0
net.ipv4.conf.eth0.bc_forwarding = 0
PING 198.51.100.255 (198.51.100.255) 56(84) bytes of data.
2 packets transmitted, 0 received, 100% packet loss, time 1013ms
With it switched on in r1, the packet is delivered as a broadcast on the server LAN and both hosts reply. ping points out that the replies come from addresses other than the one it pinged (DIFFERENT ADDRESS!) and marks the second reply to the same probe as DUP!. The ttl=63 instead of 64 shows that the replies crossed one router on the way back:
PING 198.51.100.255 (198.51.100.255) 56(84) bytes of data.
64 bytes from 198.51.100.53: icmp_seq=1 ttl=63 time=0.290 ms (DIFFERENT ADDRESS!)
64 bytes from 198.51.100.10: icmp_seq=1 ttl=63 time=0.292 ms (DUP!) (DIFFERENT ADDRESS!)
64 bytes from 198.51.100.53: icmp_seq=2 ttl=63 time=1.86 ms (DIFFERENT ADDRESS!)
--- 198.51.100.255 ping statistics ---
2 packets transmitted, 2 received, +1 duplicates, 0% packet loss, time 1028ms
IPv6 has no broadcast addresses at all. Everything IPv4 does with broadcast, IPv6 does with multicast, which reaches only the hosts that asked to receive it.
Common questions
What's the difference between a subnet mask and a prefix length?
They're two ways of writing the same thing. 255.255.255.192 has 26 one bits, so it's /26. Masks appear in older tools and Windows dialogs; prefix lengths appear in nearly everything modern, including ip addr, routing tables and firewall rules.
How many hosts fit in a /n subnet?
2^(32−n) − 2 for prefixes up to /30, because the network and broadcast addresses can't be assigned. A /31 has 2 usable addresses on a point-to-point link, and a /32 is a single address, used for loopbacks and host routes (routes to one single address).
Does the router have to be the first address (.1) of the subnet?
No. It's only a convention. A gateway can have any usable address in the subnet. Some organizations use the last usable address instead (for example .254 in a /24).
Can I use 192.168.0.0/16 (mask 255.255.0.0) as one network at home, even though "192.168" was class C space?
Yes. Classes don't matter any more: the mask you configure decides the subnet. Problems start only if hosts on the same LAN use different masks, as in the warning above.
Is the network address (all host bits 0) ever usable?
Not on ordinary subnets: it names the subnet, and some old systems treated it as a broadcast address. On a /31 it's one of the two endpoint addresses, and on a /32 it's the only address.
Can I ping the broadcast address to find every host on my LAN?
Usually not. Most modern systems ignore broadcast pings, as the lab showed. Tools that discover hosts use ARP, which maps IPv4 addresses to hardware addresses, for IPv4 (article 21) or multicast for IPv6 instead.
What happens if I give a host the network or broadcast address of its subnet?
Linux lets you configure it, but the host then can't be reached reliably, because packets to that address are treated as broadcasts or dropped by other hosts. Always assign addresses from the usable range.
Summary
- Subnetting borrows host bits as a subnet number so an organization can divide its block locally. The rest of the Internet sees only the original block.
- A subnet mask has 1s for network bits and 0s for host bits, contiguous, so it's written as a prefix length such as /26.
- Network address = address AND mask, broadcast = address OR NOT mask, usable hosts lie between them: 2^(32−n) − 2 of them. The block-size shortcut (256 − mask byte) gets the same answers without binary.
- A host sends directly to destinations in its own subnet and via its gateway otherwise, so hosts on one LAN must agree on the mask.
- VLSM allows different prefix lengths within one block: give each subnet the smallest prefix that fits, start each on a multiple of its size, and allocate the biggest first.
- /31 (and /127 in IPv6) is the standard for point-to-point links.
- 255.255.255.255 is the limited broadcast, never forwarded. Directed broadcasts are off by default on routers since the smurf attacks, and hosts ignore broadcast pings. IPv6 has no broadcast.
Practice questions
1. Find the network address, broadcast address and usable range of 192.0.2.200/27.
Answer
A /27 mask is 255.255.255.224, block size 32. Blocks in the last byte start at 0, 32, 64, 96, 128, 160, 192, 224, and 200 falls in the block starting at 192. Network 192.0.2.192, broadcast 192.0.2.223, usable 192.0.2.193 to 192.0.2.222 (30 addresses).
2. What prefix length do you need for a LAN of 500 hosts, and what is its mask?
Answer
You need at least 502 addresses (500 hosts plus network and broadcast). 2⁹ = 512 is enough, so 9 host bits and a /23 prefix. The mask is 255.255.254.0.
3. Are 203.0.113.62/26 and 203.0.113.65/26 on the same subnet?
Answer
No. With a /26, blocks start at .0, .64, .128 and .192. .62 is in the .0–.63 block (network 203.0.113.0) and .65 is in the .64–.127 block (network 203.0.113.64). They need a router to talk to each other.
4. Split 192.0.2.0/24 for three LANs of 60, 28 and 12 hosts, plus one point-to-point link, allocating the biggest first.
Answer
60 hosts need 62 addresses, so a /26: 192.0.2.0/26. 28 hosts need 30, so a /27: 192.0.2.64/27. 12 hosts need 14, so a /28: 192.0.2.96/28. The link takes a /31: 192.0.2.112/31. Everything from 192.0.2.114 up is still free.
5. Why do routers not forward directed broadcasts by default?
Answer
Because a single forged packet sent to a remote subnet's broadcast address made every host on that subnet reply to the forged source, multiplying traffic toward a victim (the smurf attack). Since 1999 the standard says routers must disable forwarding of directed broadcasts by default.
6. A host is configured as 198.51.100.77/26 with gateway 198.51.100.1. What's wrong?
Answer
The gateway isn't on the host's subnet. 198.51.100.77/26 is in the subnet 198.51.100.64/26 (usable .65 to .126), and .1 is outside it, so the host can't reach the gateway directly. Either the mask is wrong or the gateway should be an address in .65–.126.
7. How many /26 subnets fit in a /22?
Answer
26 − 22 = 4 extra bits, so 2⁴ = 16 subnets.
Previous: IPv4 Addresses: Dotted Quads, Binary and the Old Address Classes
Next: CIDR and Route Aggregation: Prefixes and Longest Match

More in TCP/IP Course
Quiz: TCP/IP Delicate Details
Twenty-five questions on the details that catch out experienced engineers: sequence numbers for SYN and FIN, TIME_WAIT versus tcp_fin_timeout, Karn's algorithm, socket buffer doubling, even and odd ephemeral ports, IPv6 checksums and fragmentation, negative DNS caching, and capture filter traps.
Quiz: TCP/IP Interview Questions
Thirty-three networking questions of the kind asked in developer, SRE and network engineering interviews: what happens when you load a URL, TCP versus UDP, handshakes, closing and TIME_WAIT, subnetting, NAT, DNS, MTU problems, congestion, TLS and QUIC, and debugging slow or failing connections.
Quiz: TCP/IP Course Final Exam (All Parts)
The final exam for the TCP/IP course: thirty questions across all fourteen parts, from packets and addressing through Ethernet, ARP, IP, DHCP, NAT, ICMP, UDP, DNS, TCP connections, reliability, congestion control, security, QUIC and troubleshooting.
Quiz: Modern Transports and Troubleshooting (TCP/IP Part 14)
Review modern transports and troubleshooting: QUIC packets, handshakes, 0-RTT, streams and connection migration, HTTP/3, Multipath TCP, SCTP multihoming, and a layered method for diagnosing real network faults.
Discussion
Sign in to ask a question or share what you learned.