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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:28:03+00:00 2026-05-12T20:28:03+00:00

Suppose a bit sequence of size M, and another bit sequence of size N,

  • 0

Suppose a bit sequence of size M, and another bit sequence of size N, with M >> N. Both M and N can be saved inside integer arrays: If N has a length of 30 then an array with only one integer will be needed, but if N has a length of 300 then an array with 10 integers will be needed to store it.

What I am trying to do is to shift N inside M, and for each possible position k inside M to find the number of differences (by XORing) between N and M(k). If M has 10000 bits and N has 100 bits then there are 10000-100=9900 positions in which an XOR comparison will be performed.

Are you aware of a library that could do that or maybe propose an algorithm ? I know that it can be done with many other ways however I believe that the fastest possible method is the one proposed here. If you can think of a faster way then I’m open to suggestions !

I’d prefer something in C or C++ but other languages, even pseudocode are also acceptable.

Thanks in advance.

  • 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-12T20:28:03+00:00Added an answer on May 12, 2026 at 8:28 pm

    Here’s a complete and working solution. Minor sloppiness left as an exercise for the reader 🙂

    #include "stdio.h"
    #include "stdlib.h"
    #include "string.h"
    
    #define M_SIZE 100
    #define N_SIZE 25
    #define bitsToBytes(n) ((n + 7)/8)
    
    typedef unsigned char byte;
    
    void dumpBytes(byte *arr, size_t size) {
       int b;
       for (b=0; b<size; b++) {
          printf("%02x", *arr++);
       }
       printf("\n");
    }
    
    int main(int argc, char *argv[]) {
    
       byte M[bitsToBytes(M_SIZE)], N[bitsToBytes(N_SIZE)];
    
       /* Fill M and N with random bits */
    
       int b;
    
       for (b=0; b<sizeof(M); b++) {
          M[b] = 0xff & rand();
       }
       for (b=0; b<sizeof(N); b++) {
          N[b] = 0xff & rand();
       }
    
       /* Create a couple of arrays big enough for M_SIZE + N_SIZE, 
          to allow shifting N all the way before the left of M. */
       #define MN_SIZE (M_SIZE + N_SIZE)
       #define MN_BYTES (bitsToBytes(MN_SIZE))
       byte MM[MN_BYTES], NN[MN_BYTES];
    
       /* Zero out MM, NN, then copy M, N there (right justified). */
       int offset = sizeof(MM) - sizeof(M);
       memset (MM, 0, sizeof(MM)); memcpy(MM+offset, M, sizeof(M));
       offset = sizeof(NN) - sizeof(N);
       memset (NN, 0, sizeof(NN)); memcpy(NN+offset, N, sizeof(N));
    
       dumpBytes(MM, MN_BYTES);
       /* Count up "difference bits" until NN has been left-shifted into oblivion. */
       int s;
       for (s=0; s<MN_SIZE; s++) {
          int sum = 0;
          for (b=0; b<MN_BYTES; b++) {
      int xor = MM[b] ^ NN[b];
      while (xor != 0) {
         sum += (xor & 1);
         xor >>= 1;
      }
          }
          dumpBytes(NN, MN_BYTES);
          printf("Shift: %4d; bits: %3d.\n", s, sum);
          /* shift NN one bit to the left */
          for (b=0; b<MN_BYTES; b++) {
      NN[b] <<= 1;
      if (b < (MN_BYTES - 1) && ((NN[b+1] & 0x80) != 0)) NN[b] |= 1;
          }
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a table like: Tab(MyDate datetime null, MyIs bit null, ...) Then
Suppose I have this line of code, which loads 16 x 8-bit unsigned integer
Suppose I have an array of data, can 2 threads safely write to different
Suppose I want to find 2nd bit in binary equivalent of 13 (binary :
Suppose the accumulater register of the processor is of 16 bit , now we
This is a bit of a longshot. Suppose I am sampling signal at a
I have a 64-bit i7 machine. Suppose I allocate memory for n 32-bit integers.
Particularly the bit about live.... jQuery('.something').live('click', function() { jQuery(this).parent('form').reset(); }); I suppose this might
So lets suppose we have an action in a controller that looks a bit
Actually i m little bit confused in Intent. Suppose i have three activities. A,b,c

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.