diff --git a/bit_difference.py b/bit_difference.py index f6fe3f0..263660d 100644 --- a/bit_difference.py +++ b/bit_difference.py @@ -2,20 +2,16 @@ import sys -def xor_count(a, b): - xor = a ^ b - i = 0 - while xor != 0: - i = i + (xor & 1) - xor = xor >> 1 - return i - - def bitwise_difference(A): + l = len(A) s = 0 - for i in range(len(A)): - for j in range(i+1,len(A)): - s = s + xor_count(A[i], A[j]) + for i in range(32): + n1 = 0 + for j in range(l): + n1 += A[j] & 1 + A[j] = A[j] >> 1 + n0 = l - n1 + s += n1 * n0 return s