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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:59:54+00:00 2026-05-12T16:59:54+00:00

I have a couple uint8_t arrays in my c code, and I’d like to

  • 0

I have a couple uint8_t arrays in my c code, and I’d like to compare an arbitrary sequence bits from one with another. So for example, I have bitarray_1 and bitarray_2, and I’d like to compare bits 13 – 47 from bitarray_1 with bits 5-39 of bitarray_2. What is the most efficient way to do this?

Currently it’s a huge bottleneck in my program, since I just have a naive implementation that copies the bits into the beginning of a new temporary array, and then uses memcmp on them.

  • 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-12T16:59:55+00:00Added an answer on May 12, 2026 at 4:59 pm

    three words: shift, mask and xor.

    shift to get the same memory alignment for both bitarray. If not you will have to shift one of the arrays before comparing them. Your exemple is probably misleading because bits 13-47 and 5-39 have the same memory alignment on 8 bits addresses. This wouldn’t be true if you were comparing say bits 14-48 with bits 5-39.

    Once everything is aligned and exceeding bits cleared for table boundaries a xor is enough to perform the comparison of all the bits at once. Basically you can manage to do it with just one memory read for each array, which should be pretty efficient.

    If memory alignment is the same for both arrays as in your example memcmp and special case for upper and lower bound is probably yet faster.

    Also accessing array by uint32_t (or uint64_t on 64 bits architectures) should also be more efficient than accessing by uint8_t.

    The principle is simple but as Andrejs said the implementation is not painless…

    Here is how it goes (similarities with @caf proposal is no coincidence):

    /* compare_bit_sequence() */
    int compare_bit_sequence(uint8_t s1[], unsigned s1_off, uint8_t s2[], unsigned s2_off,
        unsigned length)
    {
    const uint8_t mask_lo_bits[] =
        { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
    const uint8_t clear_lo_bits[] =
        { 0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80, 0x00 };
    uint8_t v1;
    uint8_t * max_s1;
    unsigned end;
    uint8_t lsl;
    uint8_t v1_mask;
    int delta;
    
    /* Makes sure the offsets are less than 8 bits */
    s1 += s1_off >> 3;
    s1_off &= 7;
    
    s2 += s2_off >> 3;
    s2_off &= 7;
    
    /* Make sure s2 is the sequence with the shorter offset */
    if (s2_off > s1_off){
        uint8_t * tmp_s;
        unsigned tmp_off;
        tmp_s = s2; s2 = s1; s1 = tmp_s;
        tmp_off = s2_off; s2_off = s1_off; s1_off = tmp_off;
    }
    delta = s1_off;
    
    /* handle the beginning, s2 incomplete */ 
    if (s2_off > 0){
        delta = s1_off - s2_off;
        v1 = delta
           ? (s1[0] >> delta | s1[1] << (8 - delta)) & clear_lo_bits[delta]
           : s1[0];
           if (length <= 8 - s2_off){
               if ((v1 ^ *s2)
                    & clear_lo_bits[s2_off]
                    & mask_lo_bits[s2_off + length]){
                    return NOT_EQUAL;
               }
               else {
                   return EQUAL;
               }
            }
            else{
                if ((v1 ^ *s2) & clear_lo_bits[s2_off]){
                    return NOT_EQUAL;
            }
            length -= 8 - s2_off;
        }
        s1++;
        s2++;
    }
    
    /* main loop, we test one group of 8 bits of v2 at each loop */
    max_s1 = s1 + (length >> 3);
    lsl = 8 - delta;
    v1_mask = clear_lo_bits[delta];
    while (s1 < max_s1)
    {
        if ((*s1 >> delta | (*++s1 << lsl & v1_mask)) ^ *s2++)
        {
            return NOT_EQUAL;
        }
    }
    
    /* last group of bits v2 incomplete */
    end = length & 7;
    if (end && ((*s2 ^ *s1 >> delta) & mask_lo_bits[end]))
    {
        return NOT_EQUAL;
    }
    
    return EQUAL;
    

    }

    All possible optimisations are not yet used. One promising one would be to use larger chunks of data (64 bits or 32 bits at once instead of 8), you could also detect cases where offset are synchronised for both arrays and in such cases use a memcmp instead of the main loop, replace modulos % 8 by logical operators & 7, replace ‘/ 8’ by ‘>> 3’, etc., have to branches of code instead of swapping s1 and s2, etc, but the main purpose is achieved : only one memory read and not memory write for each array item hence most of the work can take place inside processor registers.

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

Sidebar

Related Questions

I'll have couple of python functions I must interface with from the assembly code.
I have couple of statements, the pseudo code would look something like this: insert
I have couple of questions about AS3 variables handling by AVM/compiler/scope .1. This code
Here's the problem: I have couple of pages which gets its content from a
I have couple of ideas in my brain which I would like to bring
I am using SLD to style output from my geoserver. I have couple types
I have couple areas with labels. When I hover an area I would like
I have couple of iPhone apps that sends an email via code. I've noticed
This is the code from stored procedure of SQL server. I have declared constant
I have couple of tags which has same name attribute. Something like this: <img

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.