You are given two numbers A and B. The task is to count the number of bits needed to be flipped to convert A to B.
Example 1:
Input: A = 10, B = 20
Output: 4
Explanation:
A  = 01010
B  = 10100
As we can see, the bits of A that need 
to be flipped are 01010. If we flip 
these bits, we get 10100, which is B.
Example 2:
Input: A = 20, B = 25
Output: 3
Explanation:
A  = 10100
B  = 11001
As we can see, the bits of A that need 
to be flipped are 10100. If we flip 
these bits, we get 11001, which is B.
Your Task: The task is to complete the function countBitsFlip() that takes A and B as parameters and returns the count of the number of bits to be flipped to convert A to B.
Expected Time Complexity: O(log N).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ A, B ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    ##Complete this function

    # Function to find number of bits needed to be flipped to convert A to B

    def countBitsFlip(self,a,b):

        ##Your code here

Solutions:
class Solution:

	def countBitsFlip(self, a, b):

		def binary(n):
			li = []
			while n:
				mod = n % 2
				n = n // 2
				li.append(mod)
			return li
		m = binary(a)
		n = binary(b)
		if len(m) > len(n):
			for i in range(len(m) - len(n)):
				n.append(0)
		elif len(n) > len(m):
			for i in range(len(n) - len(m)):
				m.append(0)
		ans = 0
		for i in range(len(m)):
			ans += m[i] ^ n[i]
		return ans


class Solution:

	def countBitsFlip(self, a, b):
		c = 0
		for i in range(32):
			if 1 << i & a != 1 << i & b:
				c += 1
		return c


class Solution:

	def countBitsFlip(self, a, b):
		a = bin(a)
		b = bin(b)
		a = a[2:]
		b = b[2:]
		while len(a) != len(b):
			if len(a) < len(b):
				a = '0' + a
			else:
				b = '0' + b
		cnt = 0
		for i in range(len(a)):
			if a[i] != b[i]:
				cnt += 1
		return cnt


class Solution:

	def countBitsFlip(self, a, b):
		return bin(a ^ b).count('1')


class Solution:

	def countBitsFlip(self, a, b):
		self.a = a
		self.b = b
		l_a = list(bin(a)[2:])
		l_b = list(bin(b)[2:])
		largo_a = len(l_a)
		largo_b = len(l_b)
		if largo_a > largo_b:
			dif = largo_a - largo_b
			for i in range(dif):
				l_b.insert(0, '0')
		if largo_b > largo_a:
			dif = largo_b - largo_a
			for i in range(dif):
				l_a.insert(0, '0')
		largo_a = len(l_a)
		largo_b = len(l_b)
		count = 0
		if largo_a == largo_b:
			for i in range(largo_a):
				if l_a[i] != l_b[i]:
					count += 1
			return count


class Solution:

	def countBitsFlip(self, a, b):
		x = a ^ b
		j = 1
		count = 0
		while j <= x:
			if j & x != 0:
				count = count + 1
			else:
				pass
			j = j << 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		x = a ^ b
		count = 0
		while x != 0:
			if x & 1 == 1:
				count += 1
			x >>= 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		a = bin(a)[2:]
		b = bin(b)[2:]
		count = 0
		(n, n1) = (len(a), len(b))
		mini = min(n, n1)
		maxi = max(n, n1)
		if mini == n:
			a = abs(n - n1) * '0' + a
		else:
			b = abs(n - n1) * '0' + b
		for i in range(maxi):
			if a[i] != b[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		c = a ^ b
		c = bin(c)
		c = str(c)
		return c.count('1')


class Solution:

	def countBitsFlip(self, a, b):
		c = 0
		while a > 0 or b > 0:
			if a & 1 != b & 1:
				c += 1
			a = a >> 1
			b = b >> 1
		return c


class Solution:

	def countBitsFlip(self, a, b):
		ans = 0
		while a > 0 or b > 0:
			if a & 1 == 0 and b & 1 != 0 or (a & 1 != 0 and b & 1 == 0):
				ans += 1
			a = a >> 1
			b = b >> 1
		return ans


class Solution:

	def countBitsFlip(self, a, b):
		n = a ^ b
		count = 0
		while n:
			n = n & n - 1
			count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		c = bin(a ^ b)[2:]
		ans = 0
		for i in c:
			if i == '1':
				ans += 1
		return ans


class Solution:

	def countBitsFlip(self, a, b):
		n = a ^ b
		cnt = 0
		while n != 0:
			if n & 1 == 1:
				cnt += 1
			n = n >> 1
		return cnt


class Solution:

	def countBitsFlip(self, a, b):
		count = 0
		while a != 0 and b != 0:
			if a % 2 == 0 and b % 2 != 0 or (a % 2 != 0 and b % 2 == 0):
				count += 1
			a //= 2
			b //= 2
		while a != 0:
			if a % 2 != 0:
				count += 1
			a //= 2
		while b != 0:
			if b % 2 != 0:
				count += 1
			b //= 2
		return count


class Solution:

	def countBitsFlip(self, a, b):
		c = a ^ b
		ans = 0
		while c > 0:
			c = c & c - 1
			ans += 1
		return ans


class Solution:

	def countBitsFlip(self, a, b):
		ans = 0
		xor = a ^ b
		while xor > 0:
			if xor & 1:
				ans += 1
			xor = xor >> 1
		return ans


class Solution:

	def countBitsFlip(self, a, b):
		num = a ^ b
		bit_cnt = 0
		while num != 0:
			num = num & num - 1
			bit_cnt += 1
		return bit_cnt


class Solution:

	def countBitsFlip(self, a, b):
		a = bin(a).replace('0b', '')
		b = bin(b).replace('0b', '')
		c = 0
		n = max(len(a), len(b))
		if n != len(a) and n == len(b):
			a = '0' * (n - len(a)) + a
		elif n == len(a) and n != len(b):
			b = '0' * (n - len(b)) + b
		for i in range(0, n):
			if a[i] != b[i]:
				c += 1
		return c


class Solution:

	def countBitsFlip(self, a, b):
		x = bin(a ^ b).replace('0b', '')
		x = [int(c) for c in x]
		return sum(x)


class Solution:

	def countBitsFlip(self, a, b):
		xor = a ^ b
		count = 0
		while xor:
			count += xor % 2
			xor >>= 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		count = 0
		x = bin(a)[2:].zfill(32)
		y = bin(b)[2:].zfill(32)
		for i in range(len(x)):
			if x[i] != y[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		if a > b:
			l = a
		else:
			l = b
		bina = bin(a)[2:]
		binb = bin(b)[2:]
		if l == a:
			while len(binb) != len(bina):
				binb = '0' + binb
		else:
			while len(bina) != len(binb):
				bina = '0' + bina
		count = 0
		for (i, v) in zip(bina, binb):
			if i != v:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		(tmp, res) = (a ^ b, 0)
		while tmp > 0:
			tmp = tmp & tmp - 1
			res += 1
		return res


class Solution:

	def countBitsFlip(self, a, b):
		(a, b) = (bin(a), bin(b))
		a = a[2:]
		b = b[2:]
		if len(a) > len(b):
			b = '0' * (len(a) - len(b)) + b
		if len(b) > len(a):
			a = '0' * (len(b) - len(a)) + a
		count = 0
		for (i, j) in zip(a, b):
			if i != j:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		count = 0
		A = bin(a)[2:].zfill(32)
		B = bin(b)[2:].zfill(32)
		for i in range(len(A)):
			if A[i] != B[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		c = a ^ b
		count = 0
		while c > 0:
			if c % 2 == 1:
				count += 1
			c = c // 2
		return count


class Solution:

	def countBitsFlip(self, a, b):
		abin = bin(a).replace('b', '')
		bbin = bin(b).replace('0b', '')
		aa = format(a, '0128b')
		bb = format(b, '0128b')
		count = 0
		for i in range(len(aa)):
			if aa[i] != bb[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		string = ''
		while a > 0:
			rem = a % 2
			a = a // 2
			string += str(rem)
		string1 = ''
		while b > 0:
			rem1 = b % 2
			b = b // 2
			string1 += str(rem1)
		diff = 0
		if len(string) > len(string1):
			diff = len(string) - len(string1)
			string1 += '0' * diff
		else:
			diff = len(string1) - len(string)
			string += '0' * diff
		count = 0
		for i in range(len(string)):
			if string[i] != string1[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		aa = bin(a)[2:]
		bb = bin(b)[2:]
		count = 0
		if len(bb) > len(aa):
			c = len(bb) - len(aa)
			for i in range(c):
				aa = '0' + aa
		else:
			c = len(aa) - len(bb)
			for i in range(c):
				bb = '0' + bb
		for i in range(len(aa)):
			if aa[i] != bb[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):

		def countbits(number):
			count = 0
			while number:
				count += 1
				number = number & number - 1
			return count
		x = a ^ b
		num2 = countbits(x)
		return num2


class Solution:

	def countBitsFlip(self, a, b):
		x = bin(a)[2:]
		y = bin(b)[2:]
		c = 0
		if len(y) > len(x):
			(x, y) = (y, x)
		for i in range(1, min(len(x), len(y)) + 1):
			c += 1 if x[-i] != y[-i] else 0
		c += x[:len(x) - len(y)].count('1')
		return c


def binary(x):
	res = ''
	while x != 0:
		res = str(x % 2) + res
		x = x // 2
	return res

class Solution:

	def countBitsFlip(self, a, b):
		a = binary(a)
		b = binary(b)
		if len(a) > len(b):
			b = '0' * (len(a) - len(b)) + b
		else:
			a = '0' * (len(b) - len(a)) + a
		count = 0
		for i in range(len(a)):
			if a[i] != b[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		A = ''
		while a > 0:
			rem = a % 2
			A = str(rem) + A
			a = a // 2
		B = ''
		while b > 0:
			rem = b % 2
			B = str(rem) + B
			b = b // 2
		if len(A) > len(B):
			diff = len(A) - len(B)
			val = '0' * diff
			B = val + B
		if len(B) > len(A):
			diff = len(B) - len(A)
			val = '0' * diff
			A = val + A
		count = 0
		for i in range(len(A)):
			if A[i] != B[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		n = a ^ b
		res = 0
		while n > 0:
			if n % 2:
				res += 1
			n = n // 2
		return res


class Solution:

	def countBitsFlip(self, a, b):
		n = a ^ b
		s = bin(n)
		c = 0
		for i in s:
			if i == '1':
				c += 1
		return c


class Solution:

	def countBitsFlip(self, a, b):
		flips = 0
		while max(a, b) > 0:
			if a & 1 != b & 1:
				flips += 1
			a = a >> 1
			b = b >> 1
		return flips


class Solution:

	def countBitsFlip(self, a, b):
		flips = 0
		while a > 0 or b > 0:
			t1 = a & 1
			t2 = b & 1
			if t1 != t2:
				flips += 1
			a >>= 1
			b >>= 1
		return flips


class Solution:

	def countBitsFlip(self, a, b):
		mx = max(a, b)
		mn = min(a, b)
		count = 0
		while mx > 0:
			if mx & 1 != mn & 1:
				count += 1
			mx = mx >> 1
			mn = mn >> 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		count = 0
		bit = 0
		while a > 0 or b > 0:
			bit1 = a % 2
			bit2 = b % 2
			if bit1 != bit2:
				count += 1
			a = a // 2
			b = b // 2
		return count


class Solution:

	def countBitsFlip(self, a, b):
		n = 0
		for i in list(bin(a ^ b))[2:]:
			n += int(i)
		return n


class Solution:

	def countBitsFlip(self, a, b):
		s = a ^ b
		cnt = 0
		while s:
			s = s & s - 1
			cnt += 1
		return cnt


class Solution:

	def countBitsFlip(self, a, b):
		i = 0
		c = 0
		while i < 32:
			if a & 1 << i != b & 1 << i:
				c = c + 1
			i = i + 1
		return c


class Solution:

	def countBitsFlip(self, a, b):
		i = 0
		d = a ^ b
		while d != 0:
			i += d & 1
			d >>= 1
		return i


class Solution:

	def countBitsFlip(self, a, b):
		bina = bin(a)[2:]
		binb = bin(b)[2:]
		d = max(len(bina), len(binb))
		bina = '0' * (d - len(bina)) + bina
		binb = '0' * (d - len(binb)) + binb
		s = ''
		for i in range(len(bina)):
			s += str(int(bina[i]) ^ int(binb[i]))
		return s.count('1')


class Solution:

	def countBitsFlip(self, a, b):
		bina = bin(a)[2:]
		binb = bin(b)[2:]
		la = len(bina)
		lb = len(binb)
		l = max(la, lb)
		bina = '0' * (l - la) + bina
		binb = '0' * (l - lb) + binb
		count = 0
		for i in range(len(bina)):
			if bina[i] != binb[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		bina = bin(a)[2:]
		binb = bin(b)[2:]
		d = len(bina) - len(binb)
		if d < 0:
			bina = '0' * abs(d) + bina
		else:
			binb = '0' * d + binb
		count = 0
		for i in range(len(bina)):
			if bina[i] != binb[i]:
				count += 1
		return count


class Solution:

	def countBitsFlip(self, a, b):
		(k, l, c, res) = (bin(a).replace('0b', ''), bin(b).replace('0b', ''), 0, 0)
		c = abs(int(len(k) - len(l)))
		m = max(len(k), len(l))
		for _ in range(c):
			if len(k) < m:
				k = '0' + k
			if len(l) < m:
				l = '0' + l
		for i in range(m):
			if k[i] != l[i]:
				res += 1
		return res