algorithm - How to determine the number of similar bits? -


I need to compare two numbers and see similarities in more important bits. I'm trying to determine the number of least significant bits.

  10111000 10111011  

Two offsets of 184 and 187 are required because only two at least significant bits are different.

  10111011 11111011  

The offsets of seven need to be 187 and 251, because the seventh minimum key bit is different.

My first idea was to XOR numbers together, by then the bit-shift is not correct until the number zero becomes flat. I think there is a better bit-wise solution for this, which does not include loops, but I have not done enough bit for me to come up with it.

The solution needs to work for any 64 bit, because my number is being stored in the form of UInt64 , it is being written in C # But its solution is a language agnostic.

Prefix <111> 11101101 11010101

6 offsets will be required. I'm trying to figure out how many bits I can remove from top.

  # include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; # Defined TO_L (SRTOL (SRTOL, SUL, NULL, 16)) Int TSB (unsigned long XA, unsigned long XB) {unsigned long V = xa ^ xb; Static infected unsigned long B [] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000L, 0xFFFFffff00000000L}; Static chess unsigned int s [] = {1, 2, 4, 8, 16, 32}; Unsigned int r = 0; #define STEP (i) \ if (v & amp; b [i]) {\ int t = s [i]; \ V & gt; & Gt; = T; \ R | = T; \} STEP (5) phase (4) phase (3) phase (2) phase (1) step (0) return r; } Ent Main (int AC, four ** AV) {return printf ("% d \ n", TSB (Avi [1]), To-L (AV [2]), 0;}  

I think it executes your algorithm and it is very fast, only 6 steps are required to see it .

 < Code> so many $ ./a.out 1f f 4 so many $ ./a.out 471234abcdabcd 981234abcdabcd 55 so many roses $ ./a.out 1deadbeef 7feedface 34  

Comments