Jus144tice
December 20th, 2008, 12:36 PM
Hi,
I am looking for the fastest way to compare two bitstrings of equal length and return the number of bits that differ between the two strings. For example:
a = 1011001110001101
b = 0011001010011100
These strings differ in 4 places, so the operation would return 4. Currently, I am taking the XOR of the two strings at each bit position, which would yield:
xor = 1000000100010001
Now, I count the number of 1's to get 4. However, I would like to be able to do this without having to loop through the bits. What I'd really like to do is treat the strings as integers and perform some sort of mathematical operation on them to quickly return the number of different bits.
For example, a and b could be represented as numbers:
a = 45965
b = 12956
I want to define some operator / algorithm (call it @), such that
45965 @ 12956 = 4
Because there are 4 differing bits between the two numbers' binary representation. I would be OK representing the data differently if it would help.
Any thoughts?
I am looking for the fastest way to compare two bitstrings of equal length and return the number of bits that differ between the two strings. For example:
a = 1011001110001101
b = 0011001010011100
These strings differ in 4 places, so the operation would return 4. Currently, I am taking the XOR of the two strings at each bit position, which would yield:
xor = 1000000100010001
Now, I count the number of 1's to get 4. However, I would like to be able to do this without having to loop through the bits. What I'd really like to do is treat the strings as integers and perform some sort of mathematical operation on them to quickly return the number of different bits.
For example, a and b could be represented as numbers:
a = 45965
b = 12956
I want to define some operator / algorithm (call it @), such that
45965 @ 12956 = 4
Because there are 4 differing bits between the two numbers' binary representation. I would be OK representing the data differently if it would help.
Any thoughts?