Skip to content
ENExplore Calculators

Longest Prefix Match

Determine which routing table entry wins for a destination using longest prefix match.

About Longest Prefix Match

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

Worked example

With the default values below, Longest Prefix Match returns Most Specific Matching Prefix of 172.16.5.0/24. Change any input above and every figure here is recalculated in your browser.

Inputs used

IP Address
172.16.5.10
Prefix List
0.0.0.0/0 172.16.0.0/12 172.16.0.0/16 172.16.4.0/22 172.16.5.0/24

Results

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. 1Values used
  2. 2Longest prefix match selection
  3. 3Most Specific Matching Prefix= 172.16.5.0/24
  4. 4Matched Prefix Length= 24 /
  5. 5Total Matching Prefixes= 5
  6. 6All 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

Assumptions and limits

  • The result is derived only from the 2 inputs listed above. Anything not among them is not part of the calculation.
  • In particular, this calculator does not take inflation, tax, fees or charges or compounding frequency into account.
  • Figures are rounded for display to whole numbers, so totals built from several rounded parts can differ slightly from the unrounded arithmetic.

How these calculators are built and tested is described on the Methodology page. If a figure looks wrong, please report it.

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.

Longest Prefix Match | Calcrivo