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 320937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:47:58+00:00 2026-05-12T08:47:58+00:00

Does anyone know of an optimized way of detecting a 37 bit sequence in

  • 0

Does anyone know of an optimized way of detecting a 37 bit sequence in a chunk of binary data that is optimal. Sure I can do a brute force compare using windowing (just compare starting with index 0+next 36 bits, increment and loop until i find it) but is there a better way? Maybe some hashing search that returns a probability that the sequence lies within a binary chunk? Or am I just pulling that out of my butt? Anyway, I’m going ahead with the brute force search, but I was curious if there was something more optimal. This is in C by the way.

  • 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-05-12T08:47:59+00:00Added an answer on May 12, 2026 at 8:47 am

    Interesting question. I assume your 37-bit sequence can begin at any point in a byte. Let’s say your sequence is represented by this:

    ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@

    If we have a byte aligned algorithm, we could see these 32-bit sequences bytes:

    BCDEFGHIJKLMNOPQRSTUVWXYZ0123456 [call this pattern w_A]
    CDEFGHIJKLMNOPQRSTUVWXYZ01234567 [w_B, etc.]
    DEFGHIJKLMNOPQRSTUVWXYZ012345678
    EFGHIJKLMNOPQRSTUVWXYZ0123456789
    FGHIJKLMNOPQRSTUVWXYZ0123456789@
    GHIJKLMNOPQRSTUVWXYZ0123456789@x
    HIJKLMNOPQRSTUVWXYZ0123456789@xx
    IJKLMNOPQRSTUVWXYZ0123456789@xxx
    

    Only these byte values – no others -could form the second third and fourth byte of a byte sequence containing the 37 bits of interest.

    This leads to a reasonably obvious implementation:

    unsigned char *p = ...; // input data
    size_t n = ...;  // bytes available
    size_t bitpos;
    
    --n; p++;
    bitpos = 0;
    
    while (n--) {
      uint32_t word = *(uint32_t*)p; // nonportable, sorry.
      bitpos += 8; // compiler should be able to optimise this variable out completely
    
      if (word == w_A) {
        if ((p[4] & 0xF0 == 789@) && (p[-1] & 1 == A)) {
          // we found the data starting at the 8th bit of p-1
          found_at(bitpos-1);
        }
      } else if (word == w_B) {
        if ((p[4] & 0xE0 == 89@) && (p[-1] & 3 == AB)) {
          // we found the data starting at the 7th bit of p-1
          found_at(bitpos-2);
        }
      } else if (word == w_C} {
         ...
      }
    ...
    }
    

    Obviously there are problems with this strategy. First, it might want to evaluate p[-1] first time around the loop, but that’s easy to fix. Second, it fetches a word from odd addresses; that wont work on some CPUs – SPARC and 68k for example. But doing so is an easy way to roll 4 comparisons into one.

    kek444’s suggestion would allow you to use a algorithm like KMP to skip forward in the data stream. However, the maximum size of the skip is not huge, so while the Turbo Boyer-Moore algorithm may reduce the number of byte comparisons by 4 or so, that may not be much of a win if the cost of a byte comparison is similar to the cost of a word comparision.

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

Sidebar

Related Questions

Does anyone know of any websites, or (preferably) downloadable packages that you can use
Does anyone know of a way of indexing regular expressions , so that I
Does anyone know of an online css optimizer / formatter that can handle css3
Does anyone know if there is a way to generate different code in the
Does anyone know of a way to contain a nonbreaking space in an html
Does anyone know of a good way to assert if a NameValueCollection is equivalent?
Does anyone know a better (shorter/more elegant) way than simply writing a loop and
does anyone know if iOS Simulator (that goes with XCode) support any kind of
Does anyone know how to calculate the complexity of a nested binary search tree?
Does anyone know if adding a pop-up when a user launches an app that

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.