<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp" -->

---
title: Bitap: my favorite string matching algorithm | daily.dev
description: An exposition of the bitap (shift-and) exact string matching algorithm, derived step by step from the naive brute-force pattern matching approach. The...
canonical: https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Bitap: my favorite string matching algorithm | daily.dev
og:description: An exposition of the bitap (shift-and) exact string matching algorithm, derived step by step from the naive brute-force pattern matching approach. The...
og:url: https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp
og:image: https://api.daily.dev/og/posts/FZg3LQqNP.png
og:image:alt: Bitap: my favorite string matching algorithm
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Bitap: my favorite string matching algorithm

**[Lobsters](https://daily.dev/sources/lobsters)** · 6 min read · 1 upvotes · 0 comments

## Summary

An exposition of the bitap (shift-and) exact string matching algorithm, derived step by step from the naive brute-force pattern matching approach. The derivation moves from a naive scan, to a streaming version tracking in-progress matches, to packing match states into a bitset, and finally to the shift-and-mask formulation that eliminates inner loops using bitwise shift and AND operations. The piece also discusses bitap's practical limitation to patterns shorter than a machine word, its asymptotic equivalence to brute force, and mentions the related shift-or variant and fuzzy-matching bitap variants.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://jo3-l.dev/posts/bitap>

## Questions this post answers

### What is the bitap (shift-and) string matching algorithm and how does it work?

Bitap is an exact string matching algorithm that represents the set of in-progress pattern matches as bits in a single machine word. Each step, all match states are advanced by shifting the bitset left by one, then invalid transitions are killed by ANDing with a precomputed validMask bitset for the character just read. It's named for shifting (<<1) then ANDing (& validMask[c]).

_Anyone comparing string-search algorithms for short patterns can dig deeper into bitap's mechanics via daily.dev._

### When is the bitap string matching algorithm efficient compared to naive brute-force search?

Bitap performs efficiently only when the pattern length is shorter than the machine word width (e.g., under 64 characters for a 64-bit word), since the active states must fit in a single integer bitset. When the pattern length is bounded by a constant, its asymptotic runtime is identical to the naive brute-force algorithm, and it can even run slower in practice due to the precomputation of validMask bitsets.

_Developers weighing algorithm trade-offs for pattern matching can track write-ups like this on daily.dev._

### What is the difference between shift-and and shift-or string matching algorithms?

Shift-and and shift-or are variants of the bitap algorithm using the same bitset-shifting technique, but shift-or inverts all the bit masks and uses bitwise OR instead of bitwise AND to kill invalid match states, which performs one less bit operation per input character than shift-and.

_Engineers optimizing bit-level algorithms can follow deep dives like this one on daily.dev._

## Similar posts on daily.dev

- [Aho-Corasick Algorithm](https://daily.dev/posts/aho-corasick-algorithm-px98yf4yo) · Lobsters · 0 upvotes · 0 comments
- [Two Bits Are Better Than One: making bloom filters 2x more accurate](https://daily.dev/posts/two-bits-are-better-than-one-making-bloom-filters-2x-more-accurate-afbqkwq5f) · Hacker News · 0 upvotes · 0 comments
- [The Shift of Theseus — Toy Bit Provenance Abstract Domain](https://daily.dev/posts/the-shift-of-theseus-toy-bit-provenance-abstract-domain-shrtb1gwj) · Planet Python · 0 upvotes · 0 comments

---

Tags: [#algorithms](https://daily.dev/tags/algorithms), [#data-structures](https://daily.dev/tags/data-structures)

[View this post on daily.dev](https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Bitap: my favorite string matching algorithm","url":"https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp"},"datePublished":"2026-09-08T17:37:11.673Z","dateModified":"2026-09-13T20:37:17.808Z","description":"An exposition of the bitap (shift-and) exact string matching algorithm, derived step by step from the naive brute-force pattern matching approach. The...","image":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","thumbnailUrl":"https://media.daily.dev/image/upload/s--58gMhC4P--/f_auto/v1722860399/public/Placeholder%2012","isAccessibleForFree":true,"articleSection":"Lobsters","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Lobsters","logo":"https://media.daily.dev/image/upload/s--tl8v_Fku--/f_auto,t_logo/v1698841318/logos/lobste.jpg","url":"https://daily.dev/sources/lobsters"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":1},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"algorithms,data-structures","timeRequired":"PT6M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Lobsters","item":"https://daily.dev/sources/lobsters"},{"@type":"ListItem","position":3,"name":"Bitap: my favorite string matching algorithm"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/bitap-my-favorite-string-matching-algorithm-fzg3lqqnp#faq","mainEntity":[{"@type":"Question","name":"What is the bitap (shift-and) string matching algorithm and how does it work?","acceptedAnswer":{"@type":"Answer","text":"Bitap is an exact string matching algorithm that represents the set of in-progress pattern matches as bits in a single machine word. Each step, all match states are advanced by shifting the bitset left by one, then invalid transitions are killed by ANDing with a precomputed validMask bitset for the character just read. It's named for shifting (\u003c\u003c1) then ANDing (\u0026 validMask[c]). Anyone comparing string-search algorithms for short patterns can dig deeper into bitap's mechanics via daily.dev."}},{"@type":"Question","name":"When is the bitap string matching algorithm efficient compared to naive brute-force search?","acceptedAnswer":{"@type":"Answer","text":"Bitap performs efficiently only when the pattern length is shorter than the machine word width (e.g., under 64 characters for a 64-bit word), since the active states must fit in a single integer bitset. When the pattern length is bounded by a constant, its asymptotic runtime is identical to the naive brute-force algorithm, and it can even run slower in practice due to the precomputation of validMask bitsets. Developers weighing algorithm trade-offs for pattern matching can track write-ups like this on daily.dev."}},{"@type":"Question","name":"What is the difference between shift-and and shift-or string matching algorithms?","acceptedAnswer":{"@type":"Answer","text":"Shift-and and shift-or are variants of the bitap algorithm using the same bitset-shifting technique, but shift-or inverts all the bit masks and uses bitwise OR instead of bitwise AND to kill invalid match states, which performs one less bit operation per input character than shift-and. Engineers optimizing bit-level algorithms can follow deep dives like this one on daily.dev."}}]}
```

