In ruby, what is the most efficient way to calculate the bit difference between two unsigned integers (e.g. the hamming distance)?
Eg, I have integer a = 2323409845 and b = 1782647144.
Their binary representations are:
a = 10001010011111000110101110110101
b = 01101010010000010000100101101000
The bit difference between the a & b is 17..
I can do a logical XOR on them, but that will give me a different integer != 17, I would then have to iterate through the binary representation of the result and tally the # of 1s.
What is the most efficient way to calculate the bit difference?
Now, does the answer change for calculating the bit difference of sequences of many ints? E.g. given 2 sequences of unsigned integers:
x = {2323409845,641760420,509499086....}
y = {uint,uint,uint...}
What is the most efficient way to calculate the bit difference between the two sequences?
Would you iterate through the sequence, or is there a faster way to calculate the difference across the entire sequence at once?
__builtin_popcount
is among the slowest methods if you don't use a compile flag – Wite