Skip to content
Calcrivo

Longest Prefix Match Calculator

Match an IP address against a list of CIDR prefixes and identify the most specific (longest) matching prefix.

Inputs

The address to match against the prefix list.

One CIDR prefix per line, e.g. 172.16.0.0/16

Most Specific Matching Prefix

172.16.5.0/24

Matched Prefix Length

24/

Total Matching Prefixes

5

All Matching Prefixes

172.16.5.0/24, 172.16.4.0/22, 172.16.0.0/16, 172.16.0.0/12, 0.0.0.0/0

Step by step

  1. Values used

    IP Address = 172.16.5.10

  2. Longest prefix match selection

    among all prefixes P where IP falls within P, select the P with the largest prefix length

  3. Most Specific Matching Prefix

    = 172.16.5.0/24

  4. Matched Prefix Length

    = 24 /

  5. Total Matching Prefixes

    = 5

  6. All Matching Prefixes

    = 172.16.5.0/24, 172.16.4.0/22, 172.16.0.0/16, 172.16.0.0/12, 0.0.0.0/0

How it works

Longest prefix match is the fundamental algorithm routers and packet-filtering systems use to decide which of several overlapping CIDR blocks best describes a given IP address: every prefix that numerically covers the address is a candidate match, and among those candidates the one with the longest (most specific) prefix length wins, since it makes the narrowest, most precise claim about that address. This calculator checks every supplied prefix against the target IP, lists every candidate that matches, and highlights the single most specific one that a real router's forwarding table lookup would select.

Formula

Longest prefix match selection

among all prefixes P where IP falls within P, select the P with the largest prefix length

P
A candidate CIDR prefix
IP
The address being matched

Frequently Asked Questions

Why can multiple prefixes match the same IP address?

CIDR blocks routinely overlap by design — a summary route like 172.16.0.0/16 and a more specific subnet like 172.16.5.0/24 both legitimately cover the same address range, since the /24 is entirely contained within the /16. Longest prefix match resolves this overlap by always preferring the smallest, most specific block that still contains the address.

Is longest prefix match used anywhere besides routing tables?

Yes — it is the core algorithm behind IP-based access control lists, firewall rule evaluation with CIDR-based rules, geolocation-by-IP databases, and any system that needs to resolve which of several overlapping network ranges most specifically describes a given address.

How do routers implement longest prefix match efficiently at high speed?

Software and hardware routers typically use specialized data structures — a binary trie (prefix tree) in software, or ternary content-addressable memory (TCAM) in hardware — that can search across every prefix length simultaneously and return the longest match in constant or near-constant time, which is essential for keeping pace with line-rate packet forwarding.

You might also need