Skip to main content
medium

IPv4 Iterators and CIDR

Also known as: IP to CIDR

Part 1: IPv4 Forward Iterator - IPv4Iterator(start_ip) starts at start_ip (inclusive) - next() returns the next IP as dotted string - When exhausted, next() returns "null"

Build practical IPv4 primitives: forward/reverse iterators, CIDR range expansion, and (finally) covering N sequential IPs with minimum CIDR blocks. This tests bitwise thinking, boundary discipline, and writing clear stateful code.

Bit ManipulationMath & Geometry

Examples

You write

IPv4Iterator: next()

  1. Example 1

    Input
    IPv4Iterator("255.255.255.254"): next x3
    returns
    ["255.255.255.254","255.255.255.255","null"]
  2. Example 2

    Input
    IPv4Iterator("0.0.0.255"): wraps octet boundary
    returns
    ["0.0.0.255","0.0.1.0","0.0.1.1"]

The pattern behind it

Bit Manipulation

I reach for it when

  • The statement bans extra space or restricts operators.
  • Numbers act as flags, masks or XOR pairs.

Say this before you type. Say what each bit means in this problem before you shift or mask anything.

All 12 Bit Manipulation problems

Meet Coco

Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.

1IPv4 Forward Iteratoreasy
2Part 2
3Part 3
4Part 4

Each locked part opens in the editor once the part before it passes.