Addresses as 32-bit numbers: binary, masks and prefixes
Read an IPv4 address as one 32-bit number, see why classful addressing gave way to CIDR, and use a mask to split network bits from host bits, count addresses and handle /31 and /32.
Contentsand lesson path
On this page
- Why it matters
- The first answer: classful blocks, their limits and CIDR
- How a mask splits an address, and how blocks are counted
- Converting masks in your head, and when not to subnet tightly
- Check your understanding
- Apply it
- Review prompt
Lessons of IPv4 Addressing & Subnetting
- Addresses as 32-bit numbers: binary, masks and prefixes Concept · 20 min
- Worked example: subnetting any prefix without a cheat sheet Worked example · 25 min
- VLSM planning: sizing, ordering and leaving room to summarize Worked example · 25 min
- Special-purpose ranges: private, CGNAT, loopback, link-local and documentation Concept · 15 min
- Lab: on-link vs off-link delivery - ARP, the gateway and ICMP in a capture Lab · 25 min
- Check: timed subnetting and addressing-plan exercise Knowledge check · 8 questions
What you will learn
- Subnet any IPv4 prefix by hand with binary and mask arithmetic, including /31 point-to-point links and /32 host routes, and check the result with a script.
Prerequisites: Networking Basics
01. Why it matters
Suppose two servers in the same rack are configured as 10.60.39.200/22 and 10.60.40.12/22. Read as dotted decimal, 39 and 40 look like neighbours, so a colleague expects the servers to talk to each other directly. They cannot. Every packet between them has to pass through a router, and if that router lacks a route, the connection fails while both servers look perfectly healthy.
The confusion comes from the notation. The four numbers between the dots are a convenience for people. Hosts and routers treat an IPv4 address as a single 32-bit number, and the line between the network part (which network the address belongs to) and the host part (which interface on that network) can fall after any of the 32 bits, not only at a dot. This lesson builds that 32-bit picture: binary place values, what a mask is, how a prefix length counts addresses, and why /31 and /32 break the usual counting rule. The next lesson turns the picture into a fast method you can use by hand.
02. The first answer: classful blocks, their limits and CIDR
The first answer to "which part is the network?" was built into the address. Under classful addressing the leading bits fixed the network size: a leading 0 meant Class A (about 16.7 million addresses), 10 meant Class B (65,536) and 110 meant Class C (256). For a small Internet this was sensible: any router could tell a network's size from its first octet, so routing updates needed no mask.
It broke down as the Internet grew, and RFC 4632 records why. Class C, at most 254 hosts, was too small for a mid-sized organisation and Class B, up to 65,534 hosts, far too large, yet Class B was the best fit available, so it began to run out. Handing out many Class C networks instead made routing tables grow faster than routers and operators could manage. Subnetting (RFC 950, 1985) already let an organisation split its own network with an address mask, but outside routers still saw only whole classes.
Classless Inter-Domain Routing (CIDR), first specified in RFC 1519 in 1993 and now described by RFC 4632, removed the classes. Every route and interface address carries an explicit prefix length /n: the top n bits are the network part and the other 32 - n bits number hosts. A prefix can describe any power-of-two block, from one address (/32) to the whole space (/0). One rule matters throughout this skill: the 1 bits of a mask are contiguous. RFC 950 allowed scattered subnet bits but recommended against them; a prefix length cannot express them.
octet 1 octet 2 octet 3 octet 4
10.60.39.200 = 00001010.00111100.001001|11.11001000
10.60.40.12 = 00001010.00111100.001010|00.00001100
mask /22 = 11111111.11111111.111111|00.00000000 = 255.255.252.0
Left of | : 22 network bits. Right of | : 10 host bits.
The network bits differ (001001 vs 001010), so the servers are on different
networks: 10.60.36.0/22 and 10.60.40.0/22.03. How a mask splits an address, and how blocks are counted
A subnet mask is the prefix length written as a 32-bit number: n ones followed by 32 - n zeros. /22 is 11111111.11111111.11111100.00000000, or 255.255.252.0. To find an address's network, a host computes a bitwise AND of address and mask: where the mask has a 1 the address bit is kept, and where it has a 0 the bit becomes 0. Only the third octet matters here. 39 AND 252 is 00100111 AND 11111100 = 00100100, which is 36, so the first server is in 10.60.36.0/22; 40 AND 252 is 40, so the second is in 10.60.40.0/22. Different results mean different networks.
Three facts fall out of the bits:
- A block with
h = 32 - nhost bits holds2^haddresses, since each host bit can be 0 or 1: a/22has2^10 = 1,024. - Every block starts at a multiple of its own size, because its host bits are all zero at the start. In the third octet a
/22moves in steps of 4 (36, 40, 44 and so on), so10.60.36.0/22runs from10.60.36.0to10.60.39.255. Lessons 2 and 3 lean on this alignment rule constantly. - On an ordinary LAN, the address with all host bits 0 names the network and the one with all host bits 1 is the broadcast address, which reaches every host on it. RFC 950 inherited this from the Assigned Numbers memo: zero means "this" and all ones means "all". That leaves
2^h - 2host addresses, 1,022 in a/22.
The same AND decides where a host sends a packet. RFC 950 (section 2.2) and RFC 1122 (section 3.3.1.1) describe the test: if the destination ANDed with the mask equals the host's own address ANDed with the mask, the destination is on a connected network and the host delivers the packet itself; otherwise it uses a gateway. Lesson 5 shows both outcomes on the wire.
What if the prefix changes by one bit? At /23 each block halves to 512 addresses: 10.60.39.200/23 is in 10.60.38.0/23, third-octet values 38 and 39. At /21 the block doubles to 2,048, 10.60.32.0 to 10.60.39.255, and the second server is still outside it, in 10.60.40.0/21. Only at /20 do the two share a block, 10.60.32.0/20. Shortening a prefix by one bit merges a block with exactly one aligned partner of the same size, so 39 and 40 stay apart until the block grows to a /20 that starts at 32.
At the far end, /31 and /32 break the - 2 rule. A /31 has two addresses. A point-to-point link between two routers has no third host to broadcast to, so RFC 3021 makes both addresses host addresses and uses the limited broadcast 255.255.255.255 on such links. That halves the cost of numbering a router link compared with a /30, which spends four addresses on two ends. RFC 3021 covers point-to-point links only, and warns that a link where only one end supports /31 may not work. A /32 is one address: a single interface such as a router loopback, or one destination in a host route.
04. Converting masks in your head, and when not to subnet tightly
Because the 1 bits of a mask are contiguous, one octet of a mask can take only nine values. Add the place values 128, 64, 32, 16, 8, 4, 2, 1 from the left: 128, 192, 224, 240, 248, 252, 254 and 255, plus 0. To convert a mask, count 8 for each 255 and add the 1 bits of the next octet: 255.255.255.240 is 8 + 8 + 8 + 4, a /28, and 255.255.248.0 is 8 + 8 + 5, a /21. Going back, /26 is three full octets plus two bits: 255.255.255.192. An octet outside the nine values means the field does not hold a normal mask. Some devices use wildcard masks, with the bits inverted, in access lists (Firewalls & ACLs), so check which kind a field expects.
With CIDR it is tempting to make every subnet as small as possible. Tight subnets are right when addresses are scarce or costly: public IPv4 space, router links (a /31 each), or a cloud range shared with many teams. Inside private RFC 1918 space they are often a false economy. Suppose a team gives a new application VLAN a /28 (14 hosts) because it has nine servers today. When the fifteenth arrives, someone must renumber every server, firewall rule and DNS record, or add a second subnet and route between the two; RFC 1918 itself lists renumbering as a real cost. Private space is plentiful, so leaving room costs little.
Very large subnets cost something too, so "a /16 everywhere" is not the answer either. Every broadcast, each ARP request included, reaches every host on the subnet, and one misbehaving host disturbs its whole broadcast domain. Our recommendation: size each subnet for realistic growth, not for today's count and not for the largest number you can imagine. Lesson 3 turns that into a plan.
05. Check your understanding
06. Apply it
Convert 255.255.255.248 and 255.255.224.0 to prefix lengths, and use each to find, by hand, the network of 172.18.95.130. List your own machine's addresses (ip -br addr on Linux, ipconfig on Windows) and work out each network from the octet where its boundary falls. Then find the longest prefix that contains both 172.18.95.130 and 172.18.100.7. These conversions are the arithmetic behind the skill's addressing-plan task.
Next, the worked example on subnetting any prefix turns this model into a fast four-step method.
07. Review prompt
Without looking back, explain why 10.60.39.200/22 and 10.60.40.12/22 are on different networks, why every block starts at a multiple of its own size, what problem CIDR solved that classful addressing could not, and why /31 and /32 do not lose two addresses to the network and broadcast.
References
Your progress
Reading state
Sign in to save that you finished reading this lesson.
Prove this skill
8 questions · about 6 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.