From f843a4acc9eab88ff4670b8cbb093e27771da2a9 Mon Sep 17 00:00:00 2001 From: Claudio Maggioni Date: Thu, 4 Apr 2019 20:25:17 +0200 Subject: [PATCH] Added H solution --- bit_difference.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) 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