{"id":145,"date":"2026-11-13T12:32:40","date_gmt":"2026-11-13T04:32:40","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bitap-matches-a-pattern-with-word-shifts\/"},"modified":"2026-11-13T21:51:16","modified_gmt":"2026-11-13T13:51:16","slug":"how-bitap-matches-a-pattern-with-word-shifts","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-bitap-matches-a-pattern-with-word-shifts\/","title":{"rendered":"How bitap matches a pattern with word shifts"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> <strong>Bitap<\/strong>, or the <strong>Shift-Or<\/strong> algorithm, matches a pattern by keeping a bitset of the pattern prefixes that match the text so far. Each text character shifts the set and masks it with the character&#8217;s precomputed bit mask. A match is the bit at the pattern&#8217;s last position. The classic form assumes the pattern fits in a machine word, so a scan is <strong>O(n)<\/strong> word operations after <strong>O(m \u00b7 \u03c3)<\/strong> preprocessing. <strong>Wu-Manber<\/strong> extends the same bits to allow a small number of errors.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">KMP stores its automaton in a failure table and follows links. If the pattern is no longer than a word, the set of active prefixes is a word, and the transition is a shift and a bitwise OR or AND. The Shift-Or convention keeps a 0 bit for an active state so that the shift-in of a new active prefix uses a single OR with a mask that has zeros where the character is allowed.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The same bits answer a different question when an extra row of bits is kept for each error count. That is approximate bitap: an insertion, deletion, or substitution moves the search into the next error row. The pattern length is still bounded by the word size, or by a few words if the implementation uses multi-word arithmetic.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Exact Shift-Or scans the text in O(n) word operations for m \u2264 word size, after building a mask for each alphabet character in O(\u03c3 + m). Extra memory is the masks. Every match is detected when the high bit that represents a full pattern becomes active. Overlapping matches fall out of the same scan, because the bitset is not reset after a hit.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Wu-Manber with at most k errors uses k+1 bit words and still scans in O(k \u00b7 n) word operations for a pattern that fits in one word. It computes edit distance only up to k and only for the pattern against windows of the text, not the full Levenshtein table between two long strings. A pattern longer than a word needs either multi-word bits or a different automaton.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>For each alphabet character, build a mask of m bits. In the Shift-Or form, bit j is 0 when pattern[j] equals that character, and 1 otherwise.<\/li>\n<li>Set the state to all ones. A 0 bit will mean a live prefix.<\/li>\n<li>For each text character, shift the state left by one, set the new low bit to 0, and OR the state with the mask of the character.<\/li>\n<li>If the bit that represents position m\u22121 is 0, the pattern matches ending at this text index.<\/li>\n<li>For k errors, keep k+1 states. The transition for error row e combines the exact shift of row e with insertions, deletions, and substitutions drawn from row e\u22121, using shifts and ORs.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The linear scan is a word-RAM bound for a short pattern. It does not replace KMP for a pattern of thousands of characters unless the bits are multi-word and the cost is stated honestly. It is not a general edit-distance table. Levenshtein between two arbitrary strings is the quadratic DP. Bitap is pattern search, exact or with a small error budget.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when the pattern is longer than the bit word<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Pack m &gt; 64 into one machine word<\/td>\n<td>The high bits fall off the shift. Matches past the word width are lost. Use multi-word arithmetic or a different algorithm.<\/td>\n<\/tr>\n<tr>\n<td>Treat a 0 in an unrelated bit as a full match<\/td>\n<td>Only the bit at position m\u22121 means the entire pattern matched. Lower bits are proper prefixes.<\/td>\n<\/tr>\n<tr>\n<td>Use the k-error transition and report it as full Levenshtein distance<\/td>\n<td>The rows stop at k. A distance above k is indistinguishable from &#8220;more than k&#8221; and is not a number.<\/td>\n<\/tr>\n<tr>\n<td>Reset the state after every hit<\/td>\n<td>Overlapping matches need the prefixes that remain live. Resetting drops a match that started inside the previous one.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Long exact patterns belong to KMP, Boyer-Moore, or a suffix array. Full edit distance belongs to the Levenshtein DP. Bitap is the word-parallel automaton for a short pattern, with Wu-Manber as its small-k extension.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment keeps a bit for each matched prefix. A 0 bit means the prefix is still live, which matches the Shift-Or orientation. The pattern <code>cat<\/code> occurs in <code>a cat<\/code> at index <code>2<\/code>.<\/p>\n\n<pre><code class=\"language-python\">def bitap(text, pat):\n    if not pat or len(pat) &gt; 63:\n        return -1\n    mask = [~0] * 128\n    for i, ch in enumerate(pat):\n        mask[ord(ch)] &amp;= ~(1 &lt;&lt; i)\n    state = ~1\n    for i, ch in enumerate(text):\n        state |= mask[ord(ch)]\n        state &lt;&lt;= 1\n        if (state &amp; (1 &lt;&lt; len(pat))) == 0:\n            return i - len(pat) + 1\n    return -1\n\nassert bitap(&quot;a cat&quot;, &quot;cat&quot;) == 2\nprint(bitap(&quot;a cat&quot;, &quot;cat&quot;))\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>agrep<\/strong> uses the bitap (shift-or) algorithm for approximate match of a short pattern: each bit of a word is one state of the automaton. A long pattern does not fit in a machine word and falls back to another matcher. Exact match of a long literal is Boyer-Moore or Aho-Corasick.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop bitap when the pattern is long. The word-sized assumption is the algorithm. Keep it for short patterns, especially when a small error budget must be scanned in one pass over a large text.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for bitap returns the shift-and-mask state and the word-size limit that bounds the pattern. <strong>Bitap matches a short pattern in linear time by shifting a bitset of live prefixes and masking it with the current character, and Wu-Manber adds a row of bits per allowed error.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: Bitap, or the Shift-Or algorithm, matches a pattern by keeping a bitset of the pattern prefixes that match the text so far. Each text character shifts the set and masks it with the character&#8217;s precomputed bit mask. A match is the bit at the pattern&#8217;s last position.&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,38],"tags":[],"class_list":["post-145","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-strings"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/145","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=145"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions"}],"predecessor-version":[{"id":544,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/145\/revisions\/544"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=145"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=145"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=145"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}