IPv4 subnetting and CIDR prefixes
Find the network, broadcast and usable range of an IPv4 address and prefix by hand, carve a block into subnets and size a subnet for a host count. IP addressing and delivery · Computer networks.
You are handed a laptop whose address is 192.168.10.77/26 and asked three things. Which network is it on? Which address is the subnet-directed broadcast? How many interface addresses are ordinarily usable? The address alone answers none of them. The /26 does, and the answers come from a few bit operations you can do by hand.
Why an address carries a slash
An IPv4 address is 32 bits, written as four decimal numbers called octets, each standing for 8 bits. The leading bits identify a prefix, and the rest distinguish addresses within that block. Early classful IPv4 fixed the network class from the first address bits: Class C used a 24-bit network number. Subnet masks later divided classful networks internally. Classless Inter-Domain Routing (CIDR), introduced in 1993, also used explicit prefix lengths for allocation and routing across class boundaries. The number after the slash is the prefix length, the count of leading bits that name the network, and it wins over whatever the first octet looks like.
The set of addresses that share those network bits is a block. The remaining 32 minus 26 = 6 bits are the host bits. They can take 2^6 = 64 values, so a /26 block holds 64 addresses. The subnet mask writes the same split as an address: 26 ones followed by 6 zeros, which is 11111111.11111111.11111111.11000000, or 255.255.255.192. Going the other way, count the ones in a mask to recover the prefix: 255.255.255.192 has 24 + 2 = 26 ones, because 192 is 11000000.
Worked example: the block of 192.168.10.77/26
The first three octets are all network bits, so only the fourth octet needs work. Write 77 and the mask's 192 in binary and AND them bit by bit, which keeps a bit only where both are 1:
77 = 01001101
192 = 11000000
AND = 01000000 = 64The six host bits are now all 0, so the network address is 192.168.10.64. Setting those six bits to 1 instead gives 01111111 = 127, the broadcast address 192.168.10.127, the subnet-directed broadcast destination. Delivery is not guaranteed: directed broadcasts can be filtered and hosts may not respond. The first address names the block and the last is the broadcast, so ordinary hosts use the addresses between them: 192.168.10.65 through 192.168.10.126, which is 64 minus 2 = 62 usable addresses.
A shortcut agrees with this. Blocks of 64 start at multiples of 64, which are 0, 64, 128 and 192, and 77 falls between 64 and 127. The first animation walks through this address.
Find the block of one address
Read the steps as text
- Which block does 192.168.10.77 fall in when the prefix length is /26?
- In binary the address is 11000000.10101000.00001010.01001101.
- A /26 prefix marks the first 26 bits as the network part, so the mask is 255.255.255.192 and 6 bits are host bits.
- A /26 ends 2 bits into the fourth octet, so the AND clears the host bits from there on: 77 AND 192 gives 64. The network address is 192.168.10.64.
- Setting all 6 host bits to 1 gives the broadcast address 192.168.10.127.
- Finished. Usable hosts run from 192.168.10.65 to 192.168.10.126: 62 addresses between the network and broadcast addresses.
Carving a block into subnets
Subnetting reuses the same split. A subnet is a block used as one network, usually one LAN, with routers between subnets.
When both interfaces use this subnet prefix, matching masked addresses place them in the same subnet. Hosts normally use routes to decide whether a destination is on-link or needs a next hop; routing tables and prefix lookup follow what the router does next.
Now cut a block. The block 192.168.50.0/24 has 8 host bits. Taking two of them for the network part makes the prefix /26, and the two borrowed bits can read 00, 01, 10 or 11. That cuts the /24 into 2^2 = 4 blocks of 64 addresses each, starting at .0, .64, .128 and .192. Each block starts at a multiple of its own size, which is called alignment, so a /26 can begin at .64 but never at .20. The second animation cuts this block.
Cut a /24 into four /26 blocks
Read the steps as text
- Cut 192.168.50.0/24 into equal blocks that each use a /26 prefix.
- A /26 prefix is 2 bits longer than the parent, so the 2 borrowed bits can take 4 values, from 00 to 11, and each value is one block of 64 addresses.
- Borrowed bits 00 give block 1, 192.168.50.0/26. Usable hosts run from 192.168.50.1 to 192.168.50.62, and 192.168.50.63 is the broadcast address.
- Borrowed bits 01 give block 2, 192.168.50.64/26. Usable hosts run from 192.168.50.65 to 192.168.50.126, and 192.168.50.127 is the broadcast address.
- Borrowed bits 10 give block 3, 192.168.50.128/26. Usable hosts run from 192.168.50.129 to 192.168.50.190, and 192.168.50.191 is the broadcast address.
- Borrowed bits 11 give block 4, 192.168.50.192/26. Usable hosts run from 192.168.50.193 to 192.168.50.254, and 192.168.50.255 is the broadcast address.
- Finished. The 4 blocks sit end to end, each with 64 addresses and 62 usable hosts.
A boundary inside the third octet
Now take 10.42.173.201/21. Twenty-one network bits fill the first two octets (16 bits) and the top 5 bits of the third, which leaves 3 host bits in the third octet and all 8 in the fourth, 11 host bits in total. The mask is 11111111.11111111.11111000.00000000, or 255.255.248.0. Only the third and fourth octets change:
173 = 10101101
248 = 11111000
AND = 10101000 = 168 201 AND 0 = 0The network address is 10.42.168.0. Setting the 11 host bits to 1 makes the third octet 10101111 = 175 and the fourth 255, so the broadcast is 10.42.175.255. Usable hosts run from 10.42.168.1 to 10.42.175.254, which is 2^11 minus 2 = 2046 addresses. Blocks in the third octet advance by 256 minus 248 = 8, so the neighbours begin at 10.42.160.0 and 10.42.176.0. The leading 10 of the address suggests a /8 to a reader who remembers old classes. The prefix says /21, and the prefix is the rule. The third animation works this address.
A prefix that ends inside the third octet
Read the steps as text
- Which block does 10.42.173.201 fall in when the prefix length is /21?
- In binary the address is 00001010.00101010.10101101.11001001.
- A /21 prefix marks the first 21 bits as the network part, so the mask is 255.255.248.0 and 11 bits are host bits.
- A /21 ends 5 bits into the third octet, so the AND clears the host bits from there on: 173 AND 248 gives 168; 201 AND 0 gives 0. The network address is 10.42.168.0.
- Setting all 11 host bits to 1 gives the broadcast address 10.42.175.255.
- Finished. Usable hosts run from 10.42.168.1 to 10.42.175.254: 2046 addresses between the network and broadcast addresses.
Sizing a subnet for a number of hosts
Often the question runs the other way. A floor needs 60 usable interface addresses in total, including its router, and you hold 192.168.50.0/24. Choose the host bits h so that 2^h minus 2 is at least 60. With h = 5 the block gives 30, which is too small. With h = 6 it gives 62, which fits, so the prefix is 32 minus 6 = /26. A /24 would also fit but would leave 254 minus 60 = 194 addresses unused, while the /26 leaves only 2. The fourth animation tries each prefix from the /24 towards longer prefixes, up to /30.
Choose the prefix for 60 hosts
Read the steps as text
- Find the longest prefix inside 192.168.50.0/24 that still has room for 60 hosts.
- Trying /24 to /30: /24, /25 and /26 each hold at least 60 hosts. A /27 is too small: it has only 30 usable addresses, and longer prefixes have fewer.
- The longest prefix that still fits is /26, with 62 usable addresses; the first block is 192.168.50.0/26.
- Finished. The block gives 62 usable addresses to 60 hosts, leaving 2 addresses unused.
Three prefixes sit at the edges. A /31 has one host bit and two addresses, and RFC 3021 lets both serve as endpoints of a point-to-point link, with no subnet-directed broadcast. A /32 denotes one address and can express a host route. A /0 covers all 2^32 addresses; 0.0.0.0/0 is used as a default route. 224.0.0.0/4 is multicast and 240.0.0.0/4 is reserved, so neither is an ordinary host block, and IPv4 datagram delivery shows how multicast differs from unicast and broadcast.
Recap
- To answer the opening question, split the address at the prefix: for
192.168.10.77/26the mask is255.255.255.192, the network is.64, the broadcast is.127and the 62 usable hosts run from.65to.126. - For an ordinary subnet, find the network by clearing host bits, the directed broadcast by setting them, and the usable count as 2^h minus 2 for h host bits; do the binary only in the octet where the prefix ends.
- Borrowing bits cuts a block into equal aligned blocks, and sizing runs the other way: pick the longest prefix whose block still holds the hosts you need.
Why CIDR also helps routing scale
A route can name a whole block, such as 10.42.168.0/21, and a shorter prefix covers a larger block. Several blocks can be advertised as one shorter prefix. That summary is exact only when the blocks fill the prefix completely and the prefix is aligned; otherwise it also covers addresses the router does not own. Routing tables and prefix lookup uses these blocks to explain summaries and how a router chooses among overlapping routes. RFC 4632 records the allocation and aggregation goals behind CIDR.
Continue with MAC, IP and ARP to see how a machine turns a neighbour's IP address into the link address it needs for the next hop.