Skip to content
Tape and Square

N-02 · Networking

CIDR calculator

Turn IPv4 or IPv6 address ranges into the fewest CIDR blocks, see the range behind any block, merge a whole list into a minimal set, and find entries that overlap.

Write 10.0.0.0/16, 10.0.0.0 255.255.0.0, 192.0.2.10 - 192.0.2.130, a single address, or the same in IPv6 (2001:db8::/48). Commas work as separators; text after # is ignored.

Merged list

6 blocks

67,203 IPv4 addresses from 5 entries

  1. 10.0.0.0/16
  2. 192.0.2.0/25
  3. 192.0.2.128/31
  4. 192.0.2.130/32
  5. 192.168.0.0/22
  6. 192.168.4.0/23

The fewest CIDR blocks that cover exactly the same addresses as the whole list: overlaps counted once, neighbours joined where they line up. IPv4 and IPv6 are merged separately.

Overlaps

  • #2 10.0.5.0/24 is inside #1 10.0.0.0/16.

Each entry

#RangeCIDR
110.0.0.0 – 10.0.255.25510.0.0.0/16
210.0.5.0 – 10.0.5.25510.0.5.0/24
3192.168.0.0 – 192.168.3.255192.168.0.0/22
4192.168.4.0 – 192.168.5.255192.168.4.0/23
5192.0.2.0 – 192.0.2.130192.0.2.0/25192.0.2.128/31192.0.2.130/32

Ranges and blocks

A CIDR block is a prefix and a length: 10.0.0.0/16 is every address that shares the first 16 bits with 10.0.0.0, 65,536 addresses in all. A block always holds a power of two addresses and always starts on a multiple of its own size, so most ranges (say, .10 to .130) aren't one block. They split into several.

Formula

Block size
2(32 − prefix)
Last address
first + size − 1
Aligned
first is a multiple of size
Range → blocks
largest aligned block from the start that fits; repeat

Taking the largest block that starts at the current address and doesn't pass the end gives the fewest blocks. A range of IPv4 addresses never needs more than 62.

Worked example

The range 192.0.2.0 – 192.0.2.130. Start at 192.0.2.0: the largest block that starts there and stays inside the range is 192.0.2.0/25 (128 addresses, .0 to .127). The next address is 192.0.2.128, and so on until the end of the range:

  • 192.0.2.0/25: 192.0.2.0 – 192.0.2.127, 128 addresses
  • 192.0.2.128/31: 192.0.2.128 – 192.0.2.129, 2 addresses
  • 192.0.2.130/32: 192.0.2.130, 1 address

3 blocks in all. No two can be joined: a block twice the size would either start before the range or run past its end.

Merging a list

To shrink a list (a firewall rule set, a route table, an allow list), the calculator joins every entry that overlaps or touches another into one stretch of addresses, then splits each stretch into blocks as above. The result covers exactly the same addresses with the fewest blocks. Two neighbouring blocks only join when they line up: 10.0.0.0/24 and 10.0.1.0/24 make 10.0.0.0/23, but 10.0.1.0/24 and 10.0.2.0/24 stay two blocks, because the /23 that would hold them both has to start at an even third octet.

The default list merges into 6 blocks. Entry #2 disappears into entry #1, and the two 192.168 entries touch but stay separate: joining them would take a block that also covers addresses the list doesn't.

Overlaps

Two CIDR blocks can never partly overlap: either they are separate, or the smaller one sits entirely inside the larger. Ranges can overlap partway, so the calculator reports three cases: the same addresses, one entry inside another, and a partial overlap with the shared addresses listed. Entries that only touch (one ends where the next begins) don't overlap.

IPv6

IPv6 entries work the same way and can sit in the same list: a prefix like 2001:db8::/48, a range, or a single address (a /128). IPv6 has no dotted masks. IPv4 and IPv6 entries are merged and compared only with their own kind. RFC 8215 gives an example of a range that takes two blocks: 64:ff9a:ffff:: – 64:ff9b::255.255.255.255 is 64:ff9a:ffff::/48 plus 64:ff9b::/96.

References

  • RFC 4632, Classless Inter-domain Routing (CIDR): prefix notation, block sizes, aggregation.
  • RFC 1918, Address Allocation for Private Internets: ranges written both as start–end and as prefixes.
  • RFC 4291, IP Version 6 Addressing Architecture; RFC 5952, IPv6 address text representation; RFC 8215 §4.2.
  • Published range splits and merges used to check the results: Python ipaddress (summarize_address_range, collapse_addresses), netaddr, Rust ipnet and Go netipx documentation; Cisco Press route-summarization examples.

Embed this calculator

Free to use on your site. Paste this where the calculator should appear; preview it.