How IPv4 Subnetting Works: Network, Broadcast and Hosts
How a prefix length and mask split an IPv4 address into network and host parts, how to find the network, broadcast and usable host range, with worked examples.
In this article
Every device on an IPv4 network has an address made of four numbers from 0 to 255, such as 192.168.1.10. Behind the numbers is a 32-bit value, and a network is a block of consecutive addresses. Subnetting is the way of describing that block: how many of the 32 bits identify the network, and how many identify the individual device. This guide explains the idea with worked examples, which you can check in the Subnet Calculator on this site. It shows the binary working, so you can see where each answer comes from.
Prefix length and mask
Today's networks are described with CIDR notation, defined in RFC 4632. A slash and a number follow the address, for example 192.168.1.0/24. The number is the prefix length, the count of leading bits that belong to the network. The same information can be written as a subnet mask, a 32-bit value with that many 1s followed by 0s. A /24 prefix is 24 ones and 8 zeros, which is 255.255.255.0. A /27 is 27 ones and 5 zeros, which is 255.255.255.224, and a /20 is 255.255.240.0. A valid mask is always a run of 1s then a run of 0s, so 255.0.255.0 is not a mask.
Finding the network address
The network address is the address with all host bits set to zero. You get it by a bitwise AND of the address and the mask. Take 10.20.30.200 with a /27 prefix. The last octet, 200, is 11001000 in binary. The /27 mask has 27 ones, so in the last octet it is 11100000, which is 224. The AND of 11001000 and 11100000 is 11000000, which is 192. The network address is 10.20.30.192/27.
Broadcast address and host range
The wildcard mask is the mask with every bit flipped; for a /27 it is 0.0.0.31. The broadcast address is the network address OR the wildcard: 192 + 31 = 223, giving 10.20.30.223. The usable host addresses lie between the network and broadcast addresses, so here they run from 10.20.30.193 to 10.20.30.222.
How many addresses
A prefix of length p leaves 32 − p host bits, so the block has 2 to the power (32 − p) addresses. A /24 has 2⁸ = 256 addresses, a /27 has 2⁵ = 32, and a /30 has 4. Two addresses in a normal subnet are reserved, the network address and the broadcast address, so usable hosts = addresses − 2. A /24 therefore has 254 usable hosts, and a /27 has 30. Two special cases exist. RFC 3021 allows a /31, with just two addresses, to be used on point-to-point links with no network or broadcast address. A /32 is a single host address.
The example from the first paragraph: 192.168.1.10/24 gives the network 192.168.1.0, the broadcast 192.168.1.255, the hosts 192.168.1.1 to 192.168.1.254, a mask of 255.255.255.0 and 254 usable hosts.
Private and public ranges
RFC 1918 sets aside three blocks for private networks that are not routed on the public internet: 10.0.0.0/8, 172.16.0.0/12 (172.16.0.0 to 172.31.255.255) and 192.168.0.0/16. Homes and offices use these and share one public address through network address translation. Addresses outside these blocks, and a few other special ranges such as loopback 127.0.0.0/8, are treated differently.
Old classes
Before CIDR, addresses were divided into classes by their first number: class A for 1 to 126, class B for 128 to 191, and class C for 192 to 223, with D for multicast and E reserved. Wikipedia explains that CIDR replaced this rigid scheme in the 1990s so blocks could be sized to need. The calculator shows the class only for reference.
- Add the prefix length to see the mask: /8 is 255.0.0.0, /16 is 255.255.0.0, /24 is 255.255.255.0
- Each step of one in the prefix halves or doubles the block
- The network address always ends in a pattern that fits the block size, for example multiples of 32 for a /27
- To split a network into smaller subnets, increase the prefix length
Sources
Share
Keep reading
Related articles
How a VPN Works: Tunnels, Encryption and Limits
A VPN wraps your traffic in an encrypted tunnel to another network. Here is how that works, which protocols are used and what a VPN cannot hide from others.
How DNS Turns a Web Address Into an IP Address
DNS is the internet's phone book. See the steps a lookup takes, from your resolver to the root, TLD and authoritative servers, and why caching matters.
Wi-Fi 7 vs Wi-Fi 6: What Actually Changes
Wi-Fi 7 doubles channel width, packs more bits into each signal and lets a device use several links at once. Here is what that means and where the limits are.