Given two non-negative integers a and b. The problem is to check whether the two numbers differ at one-bit position only or not. If yes then the answer is 1, otherwise 0.
Example 1:
Input: a = 13, b = 9
Output: 1
Explanation: (13)_{10} = (1101)_{2}
(9)_{10} = (1001)_{2}
Both the numbers differ at one bit 
position only, i.e, differ at the 
3rd bit from the right.
Example 2:
Input: a = 15, b = 8
Output: 0
Explanation: (15)_{10} = (1111)_{2} 
(8)_{10} = (1000)_{2} 
Both the numbers differ at 3 bit
positions.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function DifferOne() which takes a, b as inputs and returns the answer.
Expected Time Complexity: O(max(log a, log b))
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ a, b ≤ 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Mathematics

Starter code: #User function Template for python3



class Solution:

    def DifferOne (self, a, b):

        #complete the function here

Solutions:
class Solution:

	def DifferOne(self, a, b):
		x = max(a, b)
		y = min(a, b)
		c = bin(x)
		d = bin(y)
		if c == d:
			return 0
		else:
			m = str(c)[2:].zfill(30)
			n = str(d)[2:].zfill(30)
			s = 0
			for i in range(len(m)):
				if m[i] != n[i]:
					s += 1
				else:
					s += 0
			if s == 1:
				return 1
			else:
				return 0


class Solution:

	def DifferOne(self, a, b):
		ans = 0
		t1 = format(a, '016b')
		t2 = format(b, '016b')
		for i in range(len(t1)):
			if t1[i] != t2[i]:
				ans = ans + 1
		if ans == 1:
			return 1
		return 0


class Solution:

	def DifferOne(self, a, b):
		a = bin(a)[2:].zfill(50)
		b = bin(b)[2:].zfill(50)
		c = 0
		for i in range(50):
			if a[i] != b[i]:
				c += 1
		if c == 1:
			return 1
		return 0


class Solution:

	def DifferOne(self, a, b):
		c = a ^ b
		if c & c - 1 == 0:
			return 1
		return 0


class Solution:

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


class Solution:

	def DifferOne(self, a, b):
		c = 0
		bina = str(bin(a)[2:])
		binb = str(bin(b)[2:])
		max_len = max(len(bina), len(binb))
		bina = bina.zfill(max_len)
		binb = binb.zfill(max_len)
		for i in range(len(bina)):
			if bina[i] != binb[i]:
				c += 1
		return int(c == 1)


class Solution:

	def DifferOne(self, a, b):
		numa = bin(a)[2:]
		numb = bin(b)[2:]
		n = min(len(numa), len(numb))
		count = abs(len(numa) - len(numb))
		numa = numa[::-1]
		numb = numb[::-1]
		for i in range(n):
			count += numa[i] != numb[i]
		return int(count <= 1)


class Solution:

	def DifferOne(self, a, b):
		count = 0
		while a > 0 or b > 0:
			bita = a & 1
			bitb = b & 1
			if bita != bitb:
				count += 1
			a >>= 1
			b >>= 1
		if count == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		return 1 if bin(a ^ b)[2:].count('1') == 1 else 0


class Solution:

	def DifferOne(self, a, b):
		diff = 0
		for i in range(32):
			if a - 2 ** i == b:
				diff += 1
		if diff < 2:
			return diff
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		z = a ^ b
		if z & z - 1 == 0:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		(t, c) = (a ^ b, 0)
		if a == b:
			return 0
		while t != 0:
			if t % 2 != 0:
				c += 1
				if c > 1:
					return 0
			t = t >> 1
		return 1


class Solution:

	def DifferOne(self, a, b):
		diff = a ^ b
		if diff and (not diff & diff - 1):
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		num = a ^ b
		return int(bool(num and (not num & num - 1)))


class Solution:

	def DifferOne(self, a, b):
		k = a ^ b
		if not k & k - 1:
			return 1
		return 0


class Solution:

	def DifferOne(self, a, b):
		a = bin(a).split('b')[1]
		b = bin(b).split('b')[1]
		a_ln = len(a)
		b_ln = len(b)
		max_ln = max(a_ln, b_ln)
		reslt = []
		if a_ln < max_ln:
			n = max_ln - a_ln
			a = '0' * n + a
		if b_ln < max_ln:
			n = max_ln - b_ln
			b = '0' * n + b
		for i in range(max_ln):
			if a[i] != b[i]:
				reslt.append(1)
			else:
				reslt.append(0)
		return 1 if sum(reslt) < 2 else 0


class Solution:

	def DifferOne(self, a, b):
		mx = max(a, b)
		x = a ^ b
		return 1 if x & x - 1 == 0 else 0


class Solution:

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


class Solution:

	def DifferOne(self, a, b):
		b = bin(a ^ b)[2:]
		if int(b.strip('0')) == 1:
			return 1
		else:
			return 0


import math

class Solution:

	def DifferOne(self, a, b):
		n = a ^ b
		return int(n & n - 1 == 0)


class Solution:

	def DifferOne(self, a, b):
		binA = bin(a)[2:]
		binB = bin(b)[2:]
		while len(binA) < len(binB):
			binA = '0' + binA
		while len(binB) < len(binA):
			binB = '0' + binB
		diff = 0
		n = len(binA)
		for i in range(n):
			if binA[i] != binB[i]:
				diff += 1
		if diff == 1:
			return 1
		return 0


class Solution:

	def DifferOne(self, a, b):
		x = bin(a)
		y = bin(b)
		i = 2
		u = 0
		while i < min(len(x), len(y)):
			r = x[len(x) - i + 1]
			s = y[len(y) - i + 1]
			if int(r) != int(s):
				u = u + 1
			i += 1
		if len(x) > len(y):
			x1 = x[2:len(x) - len(y) + 2]
			u = u + x1.count('1')
		elif len(x) < len(y):
			x1 = y[2:len(y) - len(x) + 2]
			u = u + x1.count('1')
		if u == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		a = bin(a)[2:]
		b = bin(b)[2:]
		a = a[::-1]
		b = b[::-1]
		saw_diff = 0
		(n, m) = (len(a), len(b))
		for i in range(max(n, m)):
			ai = '0'
			if i < len(a):
				ai = a[i]
			bi = '0'
			if i < len(b):
				bi = b[i]
			if ai != bi:
				saw_diff += 1
		if saw_diff == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		if a < b:
			c = b
			b = a
			a = c
		a = bin(a)[2:]
		b = bin(b)[2:]
		la = len(a)
		spa = abs(la - len(b))
		b = '0' * spa + b
		d = 1
		for x in range(la):
			if a[x] == b[x]:
				pass
			else:
				d -= 1
			if d < 0:
				return 0
		return 1


class Solution:

	def DifferOne(self, a, b):
		a = str(bin(a))[2:]
		b = str(bin(b))[2:]
		if len(a) > len(b):
			while len(a) > len(b):
				b = '0' + b
		elif len(b) > len(a):
			while len(a) < len(b):
				a = '0' + a
		else:
			pass
		count = 0
		for i in range(len(a)):
			if a[i] != b[i]:
				count += 1
			else:
				pass
		if count == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		x = ''
		while True:
			if a % 2 == 0:
				x = '0' + x
			elif a % 2 == 1:
				x = '1' + x
			a = a // 2
			if a == 0:
				break
		y = ''
		while True:
			if b % 2 == 0:
				y = '0' + y
			elif b % 2 == 1:
				y = '1' + y
			b = b // 2
			if b == 0:
				break
		bool = 0
		l = abs(len(x) - len(y))
		small = ''
		big = ''
		if len(x) > len(y):
			small = y
			big = x
		elif len(x) < len(y):
			small = x
			big = y
		if l != 0:
			for i in range(l):
				small = '0' + small
		else:
			small = x
			big = y
		for i in range(len(big)):
			if big[i] != small[i]:
				bool += 1
		if bool == 0 or bool > 1:
			return 0
		return 1


class Solution:

	def DifferOne(self, a, b):
		x = bin(a).replace('0b', '')
		y = bin(b).replace('0b', '')
		y = y[::-1]
		x = x[::-1]
		diff = abs(len(y) - len(x))
		cnt = 0
		if len(y) >= len(x):
			for i in range(len(y)):
				ch = '0'
				if i < len(x):
					ch = x[i]
				if ch != y[i]:
					cnt += 1
				if cnt >= 2:
					return 0
		else:
			for i in range(len(x)):
				ch = '0'
				if i < len(y):
					ch = y[i]
				if ch != x[i]:
					cnt += 1
				if cnt >= 2:
					return 0
		return 1


class Solution:

	def DifferOne(self, a, b):
		t = ''
		s = ''
		q = 1
		while q != 0:
			r = a % 2
			s = str(r) + s
			q = a // 2
			a = q
		q = 1
		while q != 0:
			r = b % 2
			t = str(r) + t
			q = b // 2
			b = q
		a = int(s)
		b = int(t)
		f = True
		c = 0
		while a != 0 or b != 0:
			d1 = a % 10
			d2 = b % 10
			if d1 != d2:
				c = c + 1
			a = a // 10
			b = b // 10
		if c == 1:
			return '1'
		else:
			return '0'


class Solution:

	def DifferOne(self, a, b):
		c = a ^ b
		check = 0
		while c > 1:
			if c % 2 == 0:
				c = c // 2
			else:
				check = 1
				c = 0
		if check == 0:
			return 1
		else:
			return 0


def isPowerOfTwo(x):
	return x and (not x & x - 1)

class Solution:

	def DifferOne(self, x, y):
		if isPowerOfTwo(a ^ b):
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		binA = format(a, 'b')
		binB = format(b, 'b')
		differLen = len(binA) - len(binB)
		if differLen > 0:
			binB = '0' * differLen + binB
		if differLen < 0:
			binA = '0' * -differLen + binA
		count = 0
		for i in range(len(binA)):
			if binA[i] != binB[i]:
				count += 1
		if count == 1:
			return 1
		else:
			return 0


class Solution:

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


class Solution:

	def DifferOne(self, a, b):
		bit_a = str(bin(a)).replace('0b', '')
		bit_b = str(bin(b)).replace('0b', '')
		count = 0
		if len(bit_a) > len(bit_b):
			bit_b = '0' * (len(bit_a) - len(bit_b)) + bit_b
		elif len(bit_a) < len(bit_b):
			bit_a = '0' * (len(bit_b) - len(bit_a)) + bit_a
		for i in range(len(bit_a)):
			if bit_a[i] != bit_b[i]:
				count += 1
		if count == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		c = a ^ b
		counter = 0
		while c != 0:
			if c & 1 == 1:
				counter = counter + 1
			c = c >> 1
		if counter == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		(a, b) = (bin(a)[2:], bin(b)[2:])
		diff = len(a) - len(b)
		if diff > 0:
			b = '0' * diff + b
		elif diff < 0:
			a = '0' * -diff + a
		i = len(a) - 1
		differences = 0
		while i >= 0:
			if a[i] != b[i]:
				differences += 1
			if differences > 1:
				break
			i -= 1
		return int(differences == 1)


class Solution:

	def con2(self, a):
		s = ''
		while a != 0:
			s += str(a % 2)
			a //= 2
		return s

	def DifferOne(self, a, b):
		b1 = self.con2(a)
		b1 = b1[::-1]
		b2 = self.con2(b)
		b2 = b2[::-1]
		x = 0
		b1 = '0' * abs(len(b2) - len(b1)) + b1
		b2 = '0' * abs(len(b2) - len(b1)) + b2
		for i in range(len(b1)):
			if b1[i] != b2[i]:
				x += 1
		return int(x == 1 if int(True) else int(False))


class Solution:

	def DifferOne(self, a, b):
		x = list(bin(a).lstrip('0b'))
		y = list(bin(b).lstrip('0b'))
		z = 0
		if len(x) >= len(y):
			for i in range(len(x) - len(y)):
				y.insert(0, '0')
			for i in range(len(y)):
				if x[i] != y[i]:
					z += 1
		elif len(x) < len(y):
			for i in range(len(y) - len(x)):
				x.insert(0, '0')
			for i in range(len(y)):
				if x[i] != y[i]:
					z += 1
		if z == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		list_a = list(bin(a).lstrip('0b'))
		list_b = list(bin(b).lstrip('0b'))
		if abs(list_a.count('1') - list_b.count('1')) == 1:
			check = 0
			if len(list_a) < len(list_b):
				while len(list_a) < len(list_b):
					list_a = ['0'] + list_a
			elif len(list_a) > len(list_b):
				while len(list_a) > len(list_b):
					list_b = ['0'] + list_b
			for i in range(len(list_a)):
				if list_a[i] != list_b[i]:
					check += 1
			if check == 1:
				return 1
			else:
				return 0
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		(large, small) = (a, b) if a > b else (b, a)
		large = str(bin(large))[2:][::-1]
		small = str(bin(small))[2:][::-1]
		small = small + '0' * (len(large) - len(small))
		found = False
		for i in range(len(large)):
			if large[i] != small[i]:
				if found:
					return 0
				found = True
		return int(found)


class Solution:

	def DifferOne(self, a, b):
		x = a ^ b
		if x and (not x & x - 1):
			return 1
		return 0


import math

class Solution:

	def power_check(self, num):
		x = math.log2(num)
		if math.floor(x) == math.ceil(x):
			return True
		else:
			return False

	def DifferOne(self, a, b):
		x = a ^ b
		if self.power_check(x):
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		c = 0
		a = bin(a)[2:]
		b = bin(b)[2:]
		while len(a) < len(b):
			a = '0' + a
		while len(b) < len(a):
			b = '0' + b
		j = 0
		while j < len(a):
			if a[j] != b[j]:
				c += 1
				if c > 1:
					return 0
			j += 1
		return 1


class Solution:

	def DifferOne(self, a, b):
		c = a ^ b
		n = 0
		while c != 0:
			d = c % 2
			if d == 1:
				n += 1
			c = c // 2
		if n == 1:
			return 1
		else:
			return 0


class Solution:

	def DifferOne(self, a, b):
		final = a ^ b
		if final & final - 1 == 0:
			return 1
		return 0


class Solution:

	def DifferOne(self, a, b):
		s = bin(a)
		s = s[2:]
		t = bin(b)
		t = t[2:]
		d = max(len(s), len(t))
		s = (d - len(s)) * '0' + s
		t = (d - len(t)) * '0' + t
		c = 0
		for i in range(d):
			if s[i] != t[i]:
				c += 1
			if c == 2:
				return 0
		return 1
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ob = Solution()
		print(ob.DifferOne(a, b))


import math

class Solution:

	def DifferOne(self, a, b):
		return 1 if str(bin(a ^ b)).count('1') == 1 else 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ob = Solution()
		print(ob.DifferOne(a, b))


class Solution:

	def DifferOne(self, a, b):
		k = a ^ b
		if k & k - 1:
			return 0
		return 1
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(a, b) = map(int, input().split())
		ob = Solution()
		print(ob.DifferOne(a, b))


import math

class Solution:

	def DifferOne(self, a, b):
		aLength = self.getLength(a)
		bLength = self.getLength(b)
		maxLength = max(aLength, bLength)
		bitDiffer = 0
		while maxLength > 0:
			if a & 1 != b & 1:
				bitDiffer += 1
			if bitDiffer > 1:
				return 0
			a = a >> 1
			b = b >> 1
			maxLength -= 1
		return int(bitDiffer == 1)

	def getLength(self, n):
		return int(math.log2(n)) + 1


class Solution:

	def DifferOne(self, a, b):
		xor = a ^ b
		if xor & xor - 1 == 0:
			return 1
		return 0


class Solution:

	def DifferOne(self, a, b):
		res = a ^ b
		if res & res - 1:
			return 0
		return 1


class Solution:

	def DifferOne(self, a, b):
		x = a ^ b
		f = x and (not x & x - 1)
		if f == True:
			return 1
		else:
			return 0


class Solution:

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


import math

class Solution:

	def DifferOne(self, a, b):
		temp = a ^ b
		if math.log(temp, 2) == int(math.log(temp, 2)):
			return 1
		return 0


class Solution:

	def DifferOne(self, a, b):
		abin = format(a, 'b')
		bbin = format(b, 'b')
		count = 0
		count = 0
		A = B = ''
		if a > b:
			dif = len(abin) - len(bbin)
			for i in range(dif):
				B += '0'
			bbin = B + bbin
		else:
			dif = len(bbin) - len(abin)
			for i in range(dif):
				A += '0'
			abin = A + abin
		for i in range(len(abin)):
			if abin[i] != bbin[i]:
				count += 1
		if count == 1:
			return '1'
		else:
			return '0'