Introduction

Imagine you are searching a dictionary for a word you can almost remember — say algorithm — but you might have typed algorythm or algoritm. Exact search fails. You need approximate matching: find every occurrence of the pattern within k edits (substitutions, insertions, deletions) of some substring of the text.

The Bitap algorithm (also called Shift-Or or Shift-And) cracks this with a beautiful trick: it encodes the entire pattern into one bitmask per character of the alphabet, then tracks all possible match positions simultaneously by shifting and OR-ing integers. Each character in the text costs exactly one bitwise shift, one OR and one AND — and modern CPUs do those in a single clock cycle.

The algorithm was invented by Udi Manber and Ricardo Baeza-Yates and published in 1992. It remains one of the fastest practical solutions for short-to-medium patterns, and is the engine inside grep -F, several spell-checkers and bioinformatics read-mappers. Its status: a solved algorithm with proven optimal worst-case complexity for its class.

The Real Complexity

Let mm be the pattern length, nn the text length, ww the machine word width (64 on a modern CPU) and σ\sigma the alphabet size.

Exact Bitap (Shift-Or):

  • Time: O(mn/w)O(mn/w). For patterns up to 64 characters the state is one 64-bit integer, so the loop is O(n)O(n) — one shift + one OR per text character.
  • Space: O(mσ/w)O(m\sigma/w). One bitmask of length mm per distinct character.
  • Preprocessing: O(mσ)O(m\sigma) to build the character masks.

Approximate Bitap (Wu–Manber extension):

  • Adds kk extra bitmask rows — one per allowed error level.
  • Time: O(kmn/w)O(kmn/w). Still linear in nn for fixed kk and mwm \le w.
  • The hidden constant is tiny because all operations are register-level bitwise ops.

The key insight: Bitap simulates a non-deterministic finite automaton (NFA) over the pattern, but instead of explicitly stepping through every NFA state it packs all active states into the bits of an integer and advances all of them in parallel with one instruction. This is the bit-parallel trick.

Compare this with pattern matching algorithms like KMP or Boyer–Moore: those achieve O(n)O(n) for exact search on larger alphabets but don't extend as gracefully to approximate matching without rebuilding the entire automaton.

Where It Matters

Anywhere a human (or a sequencing machine) can make a mistake in a string, Bitap is a candidate:

  • agrep and fuzzy grep: the agrep command-line tool is a direct implementation of Wu–Manber Bitap. It lets you write agrep -2 'pattern' file to find lines within 2 edits of the pattern.
  • Spell-checking and autocorrect: dictionary lookup with k=1k=1 or k=2k=2 errors finds the closest word to a typo without scanning every entry naively.
  • DNA short-read alignment: sequencers produce millions of short reads (50–150 bp). Approximate Bitap can map each read to a reference genome allowing a small number of sequencing errors — the pattern fits in one 64-bit register.
  • Intrusion detection and antivirus: signature scanners must allow for slight mutations in known exploit patterns; Bitap handles that in a single pass over incoming bytes.
  • Full-text search engines: for short user queries, Bitap often outperforms index-based approaches because there is no index build time and the constant is so small.

See also pattern matching for exact algorithms, and sequence alignment for the dynamic-programming approach to longer approximate matching.

Conclusion

The Bitap algorithm is a masterclass in what happens when you let the hardware think. By compressing the entire match state into the bits of an ordinary integer, it turns a problem that looks like it requires expensive character-by-character bookkeeping into a tight loop of three cheap instructions.

It is solved and optimal for its regime: patterns up to one machine word wide, with a small number of allowed errors. Beyond that regime — long patterns, large error budgets, richer edit models — dynamic-programming methods like sequence alignment take over. But for the common case of short patterns in streaming text, no approach beats the elegance of a bit shift.

Share this article

Pick a channel — or use your device's native share sheet.

Comments

Loading comments...

https://www.kipuhub.com/en/article/bitap-algorithm/Content licensed under CC BY-NC 4.0.