I need to compare two numbers and look for similarities in more significant bits. I'm trying to determine the number of least significant bits that differ.
10111000
10111011
184 and 187 require an offset of two, because only two least significant bits differ.
10111011
11111011
187 and 251 require an offset of seven, because the seventh least significant bit differs.
My first idea was to XOR the numbers together, then bit-shift right until the number equaled zero. I feel like there is a better bit-wise solution to this that doesn't involve loops, but I haven't done enough bit-twiddling of my own to come up with it.
The solution needs to work for any 64 bits, as my numbers are being stored as UInt64
. This is being written in C#, but the solution is most likely a language agnostic one.
11101101
11010101
Would need an offset of 6 bits. I'm trying to find how many similar bits I can take off the top.