Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

IPv4 addressing, CIDR and subnetting

Practical notes on IPv4 addressing, CIDR and subnetting: how addresses, masks and prefixes work, how to compute networks, broadcasts and host counts, with worked exam-style examples.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Two or three sentences: Network engineers and embedded systems engineers use IPv4 addressing and subnetting every day to plan networks, assign addresses, configure routers and firewalls, and troubleshoot reachability. Good command of CIDR (prefixes) and subnet math is essential for exams and for real tasks like splitting a block into subnets or finding the network/broadcast of an IP.

Key ideas

All terms are defined before use.

  • IPv4 address: a 32-bit number that identifies an interface on an IP network. It is usually written as four decimal octets separated by dots (dotted decimal), e.g. 192.168.1.10. An octet is 8 bits.

  • Prefix (network mask): the number of high-order bits that belong to the network portion of the address. Written in CIDR notation as "/n" where n is an integer 0..32. Example: /24 means first 24 bits are network, last 8 bits are host.

  • Subnet mask: another way to show the prefix as a 32-bit mask in dotted decimal (e.g. 255.255.255.0 for /24). The mask has 1 bits for network and 0 bits for host.

  • Network address: the address in a subnet with all host bits set to 0. It identifies the subnet itself and is not assigned to hosts (except in special cases). Denoted as the result of bitwise-AND of IP and mask.

  • Broadcast address: the address in a subnet with all host bits set to 1. It is used to send to all hosts in the subnet.

  • Usable host addresses: addresses in a subnet between network and broadcast (host bits not all 0 and not all 1). For most subnets the count is 2^(host bits) - 2.

  • CIDR (Classless Inter-Domain Routing): a method of allocating IP addresses and routing that uses arbitrary prefix lengths instead of historical fixed classes. CIDR notation is IP/prefix.

  • Classful addressing (legacy): historical method dividing IPv4 into classes A/B/C (defaults /8, /16, /24). This is now obsolete but still appears in older textbooks and some exam questions. Remember default classes for interpretation only.

  • Private addresses (RFC 1918): address ranges reserved for private use and not routable on the public Internet: 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16. Also know: 127.0.0.0/8 is loopback (host refers to itself), 169.254.0.0/16 is link-local (APIPA), 224.0.0.0/4 are IPv4 multicast addresses, and 255.255.255.255 is the limited broadcast address.

  • VLSM (Variable Length Subnet Mask): using different subnet mask lengths inside the same major network block to allocate addresses efficiently.

  • Supernetting: combining contiguous blocks into a larger block (used in CIDR to reduce routing table size).

Exact statements, derivations and assumptions (what exam answers expect):

  1. Size formula derivation: Let prefix = p. Then host bits h = 32 - p. The total number of distinct addresses in that subnet = 2^h. For normal subnets where network and broadcast are reserved, the number of usable host addresses = 2^h - 2. (Assumes traditional IPv4 rules; /31 and /32 are special cases discussed below.)

  2. Network and broadcast calculation (bitwise derivation):

    • Write the IPv4 address as a 32-bit binary string and the mask as 32-bit mask with p ones followed by (32-p) zeros.
    • Network address = IP bitwise-AND mask (set host bits to 0).
    • Broadcast address = network address bitwise-OR with (32-p) ones (set host bits to 1).

    Exams commonly ask: draw the 32-bit binary of IP and mask, underline the network part (left p bits) and the host part (right 32-p bits). Label the network and broadcast values.

  3. Converting mask length to dotted decimal: start with p ones, pack into 4 octets. For common masks remember: /8 = 255.0.0.0, /16 = 255.255.0.0, /24 = 255.255.255.0, /30 = 255.255.255.252, /31 = 255.255.255.254, /32 = 255.255.255.255.

  4. Subnetting (splitting): to split a block into N equal subnets, choose s = ceil(log2(N)) extra prefix bits; new prefix = old prefix + s. Each new subnet will have 2^(32 - new_prefix) addresses.

  5. Combining (supernetting): to combine contiguous blocks into one bigger block, reduce prefix by r such that number of combined blocks = 2^r and addresses line up on boundaries (contiguity and alignment required).

Special cases and assumptions:

  • Traditional formula (usable = 2^h - 2) assumes network and broadcast reserved; RFC 3021 allows /31 to be used point-to-point with 2 addresses and no broadcast, and /32 identifies a single host. Mention these exceptions in answers if relevant.

Diagram to draw (textual instruction the exam expects):

  • Draw a horizontal row of 32 boxes (bits). Shade the first p boxes as 'network' and the remaining (32-p) as 'host'. Below the boxes write the dotted-decimal mask and the prefix /p. To compute network, write the IP in binary under the boxes and show bitwise AND with mask to get network (host bits become 0). To compute broadcast, change host bits to 1.

Formulas

h = 32 - p where h is number of host bits, p is prefix length in bits. Unit: bits. Applies for IPv4.

Total addresses = 2^h where h = 32 - p. Unit: count (addresses). Applies for any IPv4 subnet.

Usable hosts = 2^h - 2 where h = 32 - p (except for /31 and /32 special cases). Unit: count (addresses). Applies when network and broadcast are reserved.

Network address = IP AND mask (bitwise AND). IP and mask are 32-bit values. Unit: IPv4 address.

Broadcast address = Network OR (~mask) where ~mask inverts mask bits. Unit: IPv4 address.

To convert prefix p to dotted decimal mask: produce 32-bit value with p ones then (32-p) zeros; split into four octets and convert each octet to decimal.

Worked examples

Example 1 — Find smallest subnet for 200 hosts

  1. Requirement: 200 hosts (usable addresses). Use Usable hosts = 2^h - 2.
  2. Solve 2^h - 2 >= 200. Try h = 7 => 2^7 - 2 = 126 < 200. h = 8 => 2^8 - 2 = 254 >= 200.
  3. So need h = 8 host bits. Then prefix p = 32 - h = 24. Mask = /24 = 255.255.255.0.
  4. Final answer: a /24 subnet supports 254 usable hosts, so use /24 (255.255.255.0).

Example 2 — Given IP and prefix, find network, broadcast and usable range Problem: IP = 192.168.100.130, prefix = /26.

  1. p = 26 ⇒ h = 6, total addresses = 2^6 = 64, usable = 64 - 2 = 62.
  2. Mask /26 in dotted decimal = 255.255.255.192 (because 26 ones = 3 full octets + 2 ones in last octet => last octet binary 11000000 = 192).
  3. Write the last octet of IP: 130 decimal = 10000010 (binary). With /26, host bits are last 6 bits. Zero out host bits to get network last octet: 10000000 (128). So network = 192.168.100.128.
  4. Broadcast: set host bits to 1 => last octet 10000000 OR 00111111 = 10111111 (binary) = 191 decimal. Broadcast = 192.168.100.191.
  5. Usable host range = 192.168.100.129 — 192.168.100.190. Final answers: Network 192.168.100.128/26; Broadcast 192.168.100.191; Usable 192.168.100.129–192.168.100.190 (62 addresses).

Example 3 — Subnet a /24 into 6 equal subnets (exam-style)

  1. Original: 192.168.1.0/24. Need N = 6 subnets of equal size.
  2. s = ceil(log2(6)) = 3 extra prefix bits. New prefix = 24 + 3 = /27.
  3. Each /27 has h = 32 - 27 = 5 bits ⇒ 2^5 = 32 total addresses, usable = 30.
  4. The /27 subnets start every 32 addresses: ranges: 192.168.1.0/27 (0–31), 192.168.1.32/27 (32–63), 192.168.1.64/27, ...
  5. For example, the 3rd subnet (indexing from 1) is 192.168.1.64/27 with usable hosts 192.168.1.65–192.168.1.94, broadcast 192.168.1.95.
  6. Final: You will have 8 equal /27 subnets, the first 6 are usable as requested; each /27 supports 30 hosts.

Common mistakes

  • Mixing up prefix p and host bits h (use h = 32 - p explicitly).
  • Wrong dotted-decimal mask conversion: compute binary mask first, then decimal.
  • When finding network address, students sometimes zero the wrong bits; always show binary or use bitwise AND explicitly.
  • Not checking alignment when supernetting: combined blocks must be contiguous and aligned on the bigger block boundary.
  • Ignoring /31 and /32 special-case rules if the question mentions point-to-point links or single-host routes.

For your exam

Kinds of questions you will see: short definitions (define IPv4 address, CIDR, subnet mask), short notes (advantages of CIDR vs classful), derivations (derive usable-host formula), numericals (given IP/prefix find network/broadcast/usable range; find required prefix for N hosts; split a block into M subnets), and diagrams (draw 32-bit layout of network/host bits).

What a full-marks answer includes:

  • For definitions: a clear one-line definition and an example (e.g. IPv4 address: 32-bit, example 10.0.0.1).
  • For derivations: show steps h = 32 - p, then 2^h, then subtract 2, and state special cases.
  • For numericals: write the formulas used, show binary where needed (especially for network/broadcast), show unit (bits or addresses), and present final answers in dotted decimal and CIDR format.
  • For diagrams: draw 32 boxes, label network vs host bits, show an example IP and mask and the result of AND/OR.

Quick check

  1. How many usable hosts does a /29 subnet have? (Give number only.)
  2. What is the network and broadcast for 10.1.5.73/30? (Give two addresses.)
  3. Convert mask 255.255.255.224 to CIDR prefix length.

Answers: 1) 6; 2) network 10.1.5.72 broadcast 10.1.5.75; 3) /27

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?