Given two numbers M and N. The task is to find the position of the rightmost different bit in the binary representation of numbers.
Example 1: 
Input: M = 11, N = 9
Output: 2
Explanation: Binary representation of the given 
numbers are: 1011 and 1001, 
2nd bit from right is different.
Example 2:
Input: M = 52, N = 4
Output: 5
Explanation: Binary representation of the given 
numbers are: 110100 and 0100, 
5th-bit from right is different.
User Task:
The task is to complete the function posOfRightMostDiffBit() which takes two arguments m and n and returns the position of first different bits in m and n. If both m and n are the same then return -1 in this case.
Expected Time Complexity: O(max(log m, log n)).
Expected Auxiliary Space: O(1).
Constraints:
0 <= M <= 10^{9}
0 <= N <= 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:

    

    #Function to find the first position with different bits.

    def posOfRightMostDiffBit(self,m,n):

        #Your code here

Solutions:
class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1

		def binary(n):
			s = ''
			while n:
				mod = n & 1
				s += str(mod)
				n = n // 2
			return s
		s1 = binary(m)
		n1 = len(s1)
		s2 = binary(n)
		n2 = len(s2)
		if n1 < n2:
			for i in range(n2 - n1):
				s1 += '0'
		if n2 < n1:
			for i in range(n1 - n2):
				s2 += '0'
		for i in range(max(n1, n2)):
			if s1[i] != s2[i]:
				return i + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		x = m ^ n
		j = 1
		pos = 1
		if m == n:
			return -1
		while j <= x:
			if j & x != 0:
				return pos
			else:
				pos = pos + 1
			j = j << 1
		return pos


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		i = 1
		if m == n:
			return -1
		while m >= 0 and n >= 0:
			tempN = n % 2
			tempM = m % 2
			if tempN != tempM:
				return i
			else:
				n = n / 2
				m = m / 2
				i += 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		tmp = m ^ n
		return -1 if tmp == 0 else math.log(tmp & -tmp, 2) + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		res = 0
		if m == n:
			return -1
		while m % 2 == n % 2:
			res += 1
			m = m // 2
			n = n // 2
		return res + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		c = 1
		while m & 1 == n & 1:
			m = m >> 1
			n = n >> 1
			c += 1
		return c


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		binn = bin(m)[2:]
		binn1 = bin(n)[2:]
		if len(binn) > len(binn1):
			diff = len(binn) - len(binn1)
			binn1 = '0' * diff + binn1
		if len(binn1) > len(binn):
			diff = len(binn1) - len(binn)
			binn = '0' * diff + binn
		binn = binn[::-1]
		binn1 = binn1[::-1]
		for i in range(len(binn)):
			if binn[i] != binn1[i]:
				return i + 1
		return -1


class Solution:

	def pos(self, n):
		if n == 0:
			return 0
		i = 1
		while n % 2 == 0:
			i += 1
			n = n >> 1
		return i

	def posOfRightMostDiffBit(self, m, n):
		if m != n:
			return self.pos(m ^ n)
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		ans = m ^ n
		c = 0
		while ans:
			if ans | 1 == ans:
				c += 1
				break
			ans >>= 1
			c += 1
		if c >= 1:
			return c
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		cnt = 1
		while m & 1 == n & 1:
			cnt += 1
			m = m >> 1
			n = n >> 1
		return cnt


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		i = 1
		while m > 0 or n > 0:
			if m & 1 != n & 1:
				return i
			m = m >> 1
			n = n >> 1
			i += 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		for i in range(32):
			if m & 1 << i != n & 1 << i:
				return i + 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		xor = m ^ n
		set_bit_pos = xor & -xor
		if set_bit_pos == 0:
			return -1
		return int(math.log2(set_bit_pos) + 1)


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		t = m ^ n
		if m == n:
			return -1
		count = 1
		while m & 1 == n & 1:
			count = count + 1
			m = m >> 1
			n = n >> 1
		return count


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		(large, small) = (0, 0)
		if m > n:
			large = m
			small = n
		else:
			large = n
			small = m
		large_string = ''
		small_string = ''
		while large > 0:
			rem = large % 2
			large = large // 2
			large_string += str(rem)
		while small > 0:
			rem1 = small % 2
			small = small // 2
			small_string += str(rem1)
		if len(large_string) > len(small_string):
			diff = len(large_string) - len(small_string)
			small_string += '0' * diff
		l = len(small_string)
		l1 = len(large_string)
		for i in range(l):
			if small_string[i] != large_string[i]:
				return i + 1
		if l1 > l:
			return l + 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		i = 1
		l = max(m, n)
		s = min(m, n)
		m = l
		n = s
		while m > 0 or n > 0:
			dm = m % 2
			dn = n % 2
			if dm != dn:
				return i
			i = i + 1
			m = m // 2
			n = n // 2
		return -1


import math

class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		else:
			return int(math.log2((m ^ n) & -(m ^ n)) + 1)


from math import floor, log10

class Solution:

	def posOfRightMostDiffBit(self, m, n):
		x = m ^ n
		p = 1
		while x:
			if x & 1:
				return p
			p = p + 1
			x = x >> 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		t = m ^ n
		i = 1
		while t >= 1:
			if t % 2 == 1:
				return i
			t //= 2
			i += 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		x = m ^ n
		count = 0
		while x:
			count += 1
			if x & 1 == 1:
				return count
			else:
				x >>= 1
		return count


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		c = m ^ n
		k = bin(c)
		if m == n:
			return -1
		for i in range(len(k)):
			if k[-i] == '1':
				return i
		return c


import math

class Solution:

	def posOfRightMostDiffBit(self, m, n):
		diff = m ^ n
		if diff == 0:
			return -1
		removed_rightmost = diff & diff - 1
		ans = diff ^ removed_rightmost
		res = math.log(ans, 2) + 1
		return res


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		y = m ^ n
		if y == 0:
			return -1
		return math.ceil(math.log2(y & -y) + 1)


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		else:
			ans = int(m ^ n)
			count = int(0)
			while ans:
				count += 1
				if ans & 1:
					return count
				ans >>= 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		xor = m ^ n
		b = bin(xor)[2:]
		iteration = 0
		for i in range(len(b) - 1, -1, -1):
			iteration += 1
			if b[i] == '1':
				return iteration
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		c = m ^ n
		p = 0
		while c:
			p += 1
			if c & 1:
				return p
			c = c >> 1
		else:
			return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		out = m ^ n
		count = 0
		while out > 0:
			if out & 1:
				count += 1
				return count
			count += 1
			out >>= 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		x = bin(m)[2:]
		y = bin(n)[2:]
		a = max(len(x), len(y))
		while len(x) < a:
			x = '0' + x
		while len(y) < a:
			y = '0' + y
		for i in range(a):
			if x[a - i - 1] != y[a - i - 1]:
				return i + 1
		return -1


import math

class Solution:

	def posOfRightMostDiffBit(self, m, n):
		ans = m ^ n
		count = 0
		while ans:
			if ans & 1:
				return count + 1
			else:
				count += 1
				ans = ans >> 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		bit_pos = 1
		while m & 1 == n & 1:
			bit_pos += 1
			m >>= 1
			n >>= 1
		return bit_pos


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		result = m ^ n
		one = 1
		for i in range(1, result.bit_length() + 1):
			if result & one == one:
				return i
			one <<= 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		x = m ^ n
		if m == n or x == 0:
			return -1
		c = 1
		while x > 0:
			if x & 1:
				return c
			c += 1
			x = x >> 1


class Solution:

	def getRightMostSetBit(self, n):
		return math.log2(n & -n) + 1

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		return self.getRightMostSetBit(m ^ n)


class Solution:

	def RightMostDiffBit(self, m):
		return math.log2(m & -m) + 1

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		x = m ^ n
		count = 1
		while x > 0:
			if x & 1 == 1:
				return count
			else:
				x = x >> 1
				count += 1


import math

class Solution:

	def posOfRightMostDiffBit(self, m, n):
		temp = m ^ n
		if temp == 0 or m == n:
			return -1
		return int(math.log2(temp ^ temp & temp - 1) + 1)


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		mask = m ^ n
		if mask == 0:
			return -1
		else:
			for i in range(0, 31):
				if mask & 1 << i != 0:
					return i + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		bit_length = n.bit_length() or m.bit_length()
		count = 0
		while bit_length > 0:
			count += 1
			if n & 1 != m & 1:
				return count
			n = n >> 1
			m = m >> 1
			bit_length -= 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		m = str(bin(m))[2:]
		n = str(bin(n))[2:]
		m = m[::-1]
		n = n[::-1]
		if m == n:
			return -1
		l = max(len(m), len(n))
		m = m + '0' * (l - len(m))
		n = n + '0' * (l - len(n))
		for i in range(l):
			if m[i] != n[i]:
				return i + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		a = '{:032b}'.format(m)
		b = '{:032b}'.format(n)
		a = a[::-1]
		b = b[::-1]
		i = 0
		while i < 32 and a[i] == b[i]:
			i += 1
		return i + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if n == m:
			return -1
		bin_m = bin(m)
		bin_n = bin(n)
		bin_m = bin_m[:1:-1]
		bin_n = bin_n[:1:-1]
		if m == 0:
			return bin_n.index('1') + 1
		elif n == 0:
			return bin_m.index('1') + 1
		for i in range(0, min(len(bin_m), len(bin_n))):
			if bin_m[i] != bin_n[i]:
				return i + 1
		if len(bin_m) > len(bin_n):
			return bin_m[i:].index('1') + 2 + i
		else:
			return bin_n[i:].index('1') + 2 + i


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		res = 0
		while m or n:
			if m & 1 != 0 and n & 1 != 0 or (m & 1 == 0 and n & 1 == 0):
				m = m // 2
				n = n // 2
				res += 1
			else:
				return res + 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		m = format(m, 'b')
		n = format(n, 'b')
		if len(m) > len(n):
			n = '0' * (len(m) - len(n)) + n
		else:
			m = '0' * (len(n) - len(m)) + m
		c = 0
		i = -1
		for i in range(len(m) - 1, -1, -1):
			c += 1
			if m[i] != n[i]:
				return c
		return c


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		p = m ^ n
		if p == 0:
			return -1
		cnt = 1
		while p:
			x = p % 2
			if x == 1:
				return cnt
			cnt += 1
			p = p // 2
		return cnt


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		xor = m ^ n
		pos = 1
		if xor == 0:
			return -1
		while xor > 0:
			rem = xor % 2
			if rem == 1:
				return pos
			else:
				pos += 1
				xor = xor // 2
		return pos


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		bp = 1
		while m & 1 == n & 1:
			bp += 1
			m >>= 1
			n >>= 1
		return bp


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == 0 and n == 0 or m == n:
			return -1
		rmb = 0
		while n > 0 or m > 0:
			n_rsb = n & 1
			m_rsb = m & 1
			if n_rsb ^ m_rsb == 1:
				break
			else:
				rmb += 1
			m >>= 1
			n >>= 1
		return rmb + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		bxor = bin(m ^ n)
		pos = 1
		for i in range(len(bxor) - 1, 1, -1):
			if bxor[i] == '1':
				break
			pos += 1
		return pos


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		nn = m ^ n
		c = 1
		while nn > 0:
			if nn & 1 != 0:
				return c
			else:
				c += 1
				nn = nn // 2
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		a1 = []
		a2 = []
		m1 = m
		n1 = n
		if m == n:
			return -1
		while m != 0:
			di = m % 2
			a1.append(di)
			m //= 2
		a1 = a1[::-1]
		while n != 0:
			di = n % 2
			a2.append(di)
			n //= 2
		a2 = a2[::-1]
		if m1 == 0:
			return len(a2)
		if n1 == 0:
			return len(a1)
		c = 0
		l1 = len(a1) - 1
		l2 = len(a2) - 1
		while l1 >= 0 and l2 >= 0:
			if a1[l1] == a2[l2]:
				c += 1
				l1 -= 1
				l2 -= 1
			else:
				c += 1
				return c
		return c + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		a = str(bin(m))
		a = a[2:]
		b = str(bin(n))
		b = b[2:]
		a = a[::-1]
		l1 = len(a)
		b = b[::-1]
		l2 = len(b)
		if l1 > l2:
			temp = ''
			for i in range(l1 - l2):
				temp += '0'
			b = b + temp
		elif l2 > l1:
			temp = ''
			for i in range(l2 - l1):
				temp += '0'
			a += temp
		c = 1
		i = 0
		mini = min(len(a), len(b))
		while i < mini:
			if a[i] != b[i]:
				return c
			c += 1
			i += 1
		return i


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		m ^= n
		if m == 0:
			return -1
		count = 1
		temp = 1
		while temp < m and (not temp & m):
			count += 1
			temp <<= 1
		return count


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		xor = m ^ n
		res = xor & ~(xor - 1)
		return int(math.log(res, 2)) + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		k = m ^ n
		p = 1
		while k:
			if k & 1 == 1:
				return p
			p += 1
			k = k >> 1
		return -1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		x = m ^ n
		p = 1
		while x:
			if x & 1:
				break
			p += 1
			x >>= 1
		if m == n:
			return -1
		else:
			return p


import math

class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m == n:
			return -1
		rmdb = m ^ n
		pos = 0
		while rmdb:
			if rmdb & 1 == 0:
				pos += 1
			else:
				pos += 1
				break
			rmdb >>= 1
		return pos


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		k = m ^ n
		i = 0
		while k:
			if k & 1 == 1:
				break
			i += 1
			k >>= 1
		if m == n:
			return -1
		else:
			return i + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		if m ^ n == 0:
			return -1
		else:
			c = m ^ n
			d = 0
			while c > 0:
				if c & 1 == 1:
					break
				d += 1
				c = c >> 1
			return d + 1


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		b = []
		a = m ^ n
		c = 0
		if m == n:
			c = -1
		while a != 0:
			c += 1
			if a & 1 == 1:
				break
			a = a >> 1
		return c


class Solution:

	def posOfRightMostDiffBit(self, m, n):
		c = 0
		a = 0
		x = m ^ n
		while c == 0 and x > 0:
			if x & 1 != 0:
				c = 1
			x = x >> 1
			a += 1
		if c == 0:
			return -1
		else:
			return a