Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8462773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:15:46+00:00 2026-06-10T14:15:46+00:00

I have needed to implement a string searching algorithm that finds a pattern of

  • 0

I have needed to implement a string searching algorithm that finds a pattern of bits in a text of bits (the match may not be byte/word aligned). For starters, I implemented the Boyer-Moore algorithm, but comparing individual bits was too slow for my purposes. So instead I tried implementing a blocked based version that would compare entire bytes/words as described in this paper, but it has become complex and unmanageable (in part due to me not entirely understanding what I’m doing.)

Does anyone have good implementation of such an algorithm?

My specific use case is with pattern length N >= 32, text window 2N, and bits packed into ints. Also N in this case is a multiple of char size N % 8 == 0. I preprocess once and use many times on changing text, like the Boyer-Moore. First match is all I need. Performance is key.

Edit: After successfully implementing the Blocked Boyer-Moore algorithm, I notice no improvement(my bit by bit version is faster!) It’s probably a mistake of my own, because I’ve been racking my brain on it and optimized it to the point where it makes no sense without many lines of comments, yet it’s still slower. Here it is.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:15:48+00:00Added an answer on June 10, 2026 at 2:15 pm

    Overview

    I’m new to the SO community, but I look forward to giving something back.

    Interesting problem. I put together an implementation which only does byte-based comparisons (with the aid of pre-computed bit patterns and bit masks) as opposed to performing expensive bit-manipulations on compare. As a result, it should be reasonably fast. It doesn’t implement any of the Shift Rules (performance optimizations) discussed for the Boyer-Moore algorithm and thus could be further improved.

    Although this implementation does depend on the number of pattern bits % CHAR_BIT == 0–on an 8-bit machine, meets your criteria of N % 8 == 0, the implementation will find non-byte-aligned bit patterns. (It also currently requires 8-bit characters ( CHAR_BIT == 8 ), but in the unlikely event your system is not using 8-bit chars, is easily adaptable by changing the all of the arrays/vectors from uint8_t to char, and adjusting the values they contain to reflect the correct number of bits.)

    Given that the search doesn’t do any bit-twiddling (besides anding pre-computed byte masks) it should be pretty performant.

    Algorithm summary

    In a nutshell, the pattern to be searched for is specified, and the implementation shifts it by one bit and records the shifted pattern. It also computes masks for the shifted pattern, as for non-byte-aligned bit patterns, some bits at the beginning and end of the comparison will need to be ignored for proper behavior.

    The search is conducted for all the pattern bits in each shift position until a match is found or the end of the data buffer is reached.

    //
    //  BitStringMatch.cpp
    //
    
    #include "stdafx.h"
    #include <iostream>
    #include <cstdint>
    #include <vector>
    #include <memory>
    #include <cassert>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        //Enter text and pattern data as appropriate for your application.  This implementation assumes pattern bits % CHAR_BIT == 0
        uint8_t text[] = { 0xcc, 0xcc, 0xcc, 0x5f, 0xe0, 0x1f, 0xe0, 0x0c }; //1010 1010, 1010 1010, 1010 1010, 010*1 1111, 1110 0000, 0001 1111, 1110 0000, 000*0 1010 
        uint8_t pattern[] = { 0xff, 0x00, 0xff, 0x00 }; //Set pattern to 1111 1111, 0000 0000, 1111 1111, 0000 0000
    
        assert( CHAR_BIT == 8 ); //Sanity check
        assert ( sizeof( text ) >= sizeof( pattern ) ); //Sanity check
    
        std::vector< std::vector< uint8_t > > shiftedPatterns( CHAR_BIT, std::vector< uint8_t >( sizeof( pattern ) + 1, 0 ) );  //+1 to accomodate bit shifting of CHAR_BIT bits.
        std::vector< std::pair< uint8_t, uint8_t > > compareMasks( CHAR_BIT, std::pair< uint8_t, uint8_t >( 0xff, 0x00 ) );
    
        //Initialize pattern shifting through all bit positions
        for( size_t i = 0; i < sizeof( pattern ); ++i ) //Start by initializing the unshifted pattern
        {
            shiftedPatterns[ 0 ][ i ] = pattern[ i ];
        }
    
        for( size_t i = 1; i < CHAR_BIT; ++i )  //Initialize the other patterns, shifting the previous vector pattern to the right by 1 bit position
        {
            compareMasks[ i ].first >>= i;  //Set the bits to consider in the first...
            compareMasks[ i ].second = 0xff << ( CHAR_BIT - i ); //and last bytes of the pattern
    
            bool underflow = false;
            for( size_t j = 0; j < sizeof( pattern ) + 1; ++j )
            {
                bool thisUnderflow = shiftedPatterns[ i - 1 ][ j ] & 0x01 ? true : false; 
                shiftedPatterns[ i ][ j ] = shiftedPatterns[ i - 1][ j ] >> 1;
    
                if( underflow ) //Previous byte shifted out a 1; shift in a 1
                {
                    shiftedPatterns[ i ][ j ] |= 0x80;  //Set MSb to 1
                }
    
                underflow = thisUnderflow;
            }
        }
    
        //Search text for pattern
        size_t maxTextPos = sizeof( text ) - sizeof( pattern );
        size_t byte = 0;
        bool match = false;
        for( size_t byte = 0; byte <= maxTextPos && !match; ++byte )
        {
            for( size_t bit = 0; bit < CHAR_BIT && ( byte < maxTextPos || ( byte == maxTextPos && bit < 1 ) ); ++bit )
            {
                //Compare first byte of pattern
                if( ( shiftedPatterns[ bit ][ 0 ] & compareMasks[ bit ].first ) != ( text[ byte ] & compareMasks[ bit ].first ) )
                {
                    continue;
                }
    
                size_t foo = sizeof( pattern );
                //Compare all middle bytes of pattern
                bool matchInProgress = true;
                for( size_t pos = 1; pos < sizeof( pattern ) && matchInProgress; ++pos )
                {
                    matchInProgress = shiftedPatterns[ bit ][ pos ] == text[ byte + pos ];
                }
                if( !matchInProgress )
                {
                    continue;
                }
    
                if( bit != 0 )  //If compare failed or we're comparing the unshifted pattern, there's no need to compare final pattern buffer byte
                {
                    if( ( shiftedPatterns[ bit ][ sizeof( pattern ) ] & compareMasks[ bit ].second ) != ( text[ byte + sizeof( pattern ) ] & compareMasks[ bit ].second ) )
                    {
                        continue;
                    };
                }
    
                //We found a match!
                match = true;   //Abandon search
                std::cout << "Match found!  Pattern begins at byte index " << byte << ", bit position " << CHAR_BIT - bit - 1 << ".\n";
                break;
            }
        }
        //If no match
        if( !match )
        {
            std::cout << "No match found.\n";
        }
    
        std::cout << "\nPress a key to exit...";
        std::getchar();
    
        return 0;
    }
    

    I hope this is helpful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Up to this point we have not yet needed a new masterpage for our
I have classes that are needed in both my web service and my server.
I have a rather huge query that is needed in several stored procedures, and
I have a source code that is needed to be converted by creating classes,
Help needed.. I have a organization layout which have rectangular shapes not in an
I have multiple interfaces that my objects can implement. I am wondering if there
I have a find function that locates a string in a JTable with quite
Do you know any tools that may help to implement basic SQLite-based persistence in
I have a web form that I am attempting to implement dynamic drop down
I wrote a custom XML reader because I needed something that would not read

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.