ELEC3506

Network LayerLecture 530 min

CIDR and Subnetting

Splitting an address block into smaller ones — the AND operation, the 2^x minus 2 rule, and the two cases where that rule does not hold.

By the end of this page you should be able to

  • Find the network address by ANDing an IP address with its mask
  • Compute network, broadcast, first and last host, and usable count for any prefix
  • Explain why classful addressing wasted space and how variable-length masks fixed it
  • Handle the /31 and /32 cases, where 2^x minus 2 breaks down

The idea

Classful addressing offered exactly three sizes of network: about 16 million addresses, about 65 thousand, or 256. Nothing in between.

The lecture gives the two failure cases plainly. A network with four hosts gets a whole Class C — 256 addresses, 252 wasted. A network with 257 hosts does not fit in a Class C, so it gets a Class B — 65,536 addresses for 257 machines.

Multiply that across every organisation on the Internet and the address space runs out, not because the addresses were used but because they were allocated and sat idle.

The fix is unglamorous: stop letting the first few bits of the address dictate where the network part ends, and carry the boundary explicitly instead. That is all a subnet mask is — a statement of where the split falls. Once the boundary is free to sit anywhere, allocations can be any power of two, and a four-host site can be given eight addresses instead of 256.

The one operation

How it works

Network address = IP AND mask

A mask is 32 bits: 1 for every network bit, 0 for every host bit. AND the address with it and every host bit collapses to zero, leaving the network address.

IP address    11001101.00100000.00100101.00100100   205.16.37.36
Mask          11111111.11111111.11111111.11111000   255.255.255.248
                                             -----
AND result    11001101.00100000.00100101.00100000   205.16.37.32

That is the whole mechanism. A router does exactly this on every packet: AND the destination against each route’s mask and see which network address it matches.

The /n notation just counts the leading ones. /29 means 29 one-bits, so 255.255.255.248. There are only ever nine possible final octets, and knowing them cold turns most subnetting questions into arithmetic you can do in your head:

PrefixLast octet of maskHost bitsBlock sizeUsable
/2408256254
/251287128126
/2619266462
/2722453230
/2824041614
/29248386
/30252242
/31254122 — see below
/32255011 — see below
Block size doubles as the prefix shortens. Blocks always start at a multiple of the block size, which is the fastest way to find the network address without doing binary.

Working an example

Worked example

205.16.37.36/29 — the lecture's own example

A block is granted to a small organisation. One of its addresses is 205.16.37.36/29. Find the first address, the last address, and the total.

  1. Prefix /29 means 3 host bits. 32 − 29 = 3, so the block holds 2^3 = 8 addresses and the mask is 255.255.255.248.

  2. Find the network address. Blocks of 8 start at multiples of 8: .32, .40, .48. The address .36 sits in the block starting at .32.

    Formally, 36 AND 248:

    36    00100100
    248   11111000
    AND   00100000  = 32
  3. Network address 205.16.37.32. This one is not assignable to a host.

  4. First host is the next address up: 205.16.37.33.

  5. Broadcast is the last in the block: .32 + 8 − 1 = .39, so 205.16.37.39. Also not assignable.

  6. Last host is the one below broadcast: 205.16.37.38.

  7. Usable count: 2^3 − 2 = 6, which matches .33 through .38.

AnswerNetwork .32, hosts .33 to .38, broadcast .39, 6 usable

Worked example

192.168.10.64/28

  1. /28 leaves 4 host bits, so the block is 2^4 = 16 addresses, mask 255.255.255.240.
  2. 64 is already a multiple of 16, so it is the network address: 192.168.10.64.
  3. First host 192.168.10.65.
  4. Broadcast is 64 + 16 − 1 = 79, so 192.168.10.79.
  5. Last host 192.168.10.78.
  6. Usable: 2^4 − 2 = 14.

AnswerNetwork .64, hosts .65 to .78, broadcast .79, 14 usable

Aside

Both of the above are worked in the lecture, and the site’s subnet library is asserted against all three of its examples in lecture-examples.test.ts. If a future change ever made the calculator disagree with the unit, that test fails rather than the site quietly teaching a different answer.

Try it

Subnet explorer

Try it

The bar shows all 32 bits. Everything left of the boundary is fixed by the mask; everything right of it is yours to allocate.

Type a prefix and watch the bar split. The useful habit to build: before you read the answer, predict the block size from the prefix. /26 means 6 host bits, so 64 addresses, so blocks start at .0, .64, .128, .192. Once that is automatic, most exam questions are done in your head.

Where marks get lost

Where 2^x minus 2 stops working

The rule subtracts the network address and the broadcast address. At the two shortest blocks there is nothing left to subtract from.

/31 — two addresses. Blindly applying the formula gives 2^1 − 2 = 0, which would make the prefix pointless. RFC 3021 makes it an exception: on a point-to-point link there is exactly one device at the other end, so a broadcast address serves no purpose. Both addresses are usable. These are used heavily on router-to-router links, where a /30 wastes half its four addresses.

/32 — one address. A single host route. One address, nothing subtracted.

A question that hands you a /31 is testing whether you applied the formula or understood it.

Subnetting versus classless addressing

The lecture treats these as two related ideas and it is worth keeping them apart.

Subnetting is treating subdivisions of a single Class A, B or C network as networks in their own right. The address now has three parts rather than two: network, subnet, host. You borrow bits from the host portion, and b borrowed bits give 2^b subnets.

Classless addressing goes further and drops the class concept altogether. There is no class; a prefix length n simply states how many bits are network. The lecture’s example: /28 means the first 28 bits are network and the remaining 4 give the range, so 128.11.3.16 through 128.11.3.31 form one block.

One practical note from the lecture worth remembering — the router has to know you are using classless addressing. A router assuming classful rules would infer the boundary from the first octet and get it wrong.

Check yourself

Three to do without the widget:

  1. What is the network address of 172.16.35.200/27?
  2. How many usable hosts in a /22?
  3. A point-to-point link between two routers. Which prefix wastes least, and how many usable addresses does it give?

Answers: /27 gives blocks of 32, and 200 falls in the block starting at 192, so 172.16.35.192. A /22 has 10 host bits: 2^10 − 2 = 1022. And a /31 gives exactly the 2 usable addresses you need, versus a /30 which gives 2 out of 4.

In the exam

  • Given an address and a prefix, produce all five values — network, broadcast, first host, last host, usable count. This is the standard question and it appears in some form every year.
  • Show the AND. If the question says “show your working”, write the binary. The mask table above lets you shortcut, but the binary is what earns method marks.
  • 2^x − 2, with x as the host-bit count. State the two subtractions explicitly — network and broadcast.
  • The /31 exception. Worth knowing precisely; it is the difference between understanding the rule and reciting it.
  • Why classless exists. The four-host and 257-host examples from the lecture are the shortest way to answer it.
  • Know the mask table cold. /24 through /30 in particular. It converts a five-minute question into a one-minute one, and exam time is the scarce resource.

Check yourself

  1. An address is 205.16.37.36/29. What is the network address?
  2. How many usable host addresses are in a /28?
  3. How many usable addresses does a /31 have?
  4. A router receives a packet for 192.168.10.70 and holds a route for 192.168.10.64/28. Does the packet match?
  5. Why was classless addressing introduced?