Given two unsigned integers, what is the fastest way to count the number of matching digits in their base 4 representation?
example 1:
A= 13 = (31) in base 4
B= 15 = (33) in base 4
the number of matching digits in base 4 is 1.
example 2:
A= 163 = (2203) in base 4
B= 131 = (2003) in base 4
the number of matching digits in base 4 is 3.
The first step I guess is to calculate the bitwise XOR of the two integers, then we have to count number of 00 pairs ? what is the most efficient way t do that ?
note: assume that A and B have fixed number of digits in base 4, say exactly 16 digits.
Suppose, your ints are 4-byte each. 32 bits.
The more understandable way:
Help constant array:
Later, for the check, if
cis the integer after bitwise XOR :Other solution. Obviously, faster, but a bit less understandable:
Further qickening:
Even further:
If you really need use the function so many (10^10) times, count h[256] (you already caught, how), and use:
I think, the help array h[256*256] would be also usable yet. Then
The array of 2^16 ints will be all in the processor cash (third level, though). So, the speed will be really great.