Given N in Gray Code, find its binary equivalent. Return the decimal representation of the binary equivalent.
Example 1:
Input: N = 4
Output: 7
Explanation:
Given 4 representing gray code 110.
Binary equivalent of gray code 110 is 100.
Return 7 representing gray code 100.
Example 2:
Input: N = 15
Output: 10
Explanation:
Given 15 representing gray code 1000.
Binary equivalent of gray code 1000 is 1111.
Return 10 representing gray code 1111 
ie binary 1010.
Example 3:
Input: N = 0
Output: 0
Explanation: 
Zero remains the same in all systems.
Your Task: 
You don't need to read input or print anything. Your task is to complete the function grayToBinary() which accepts an integer n as an input parameter and returns decimal of the binary equivalent of the given gray code. 
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)
Constraints:
0 <= N <= 10^{8}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3



class Solution:    

    ##Complete this function

    # function to convert a given Gray equivalent n to Binary equivalent.

    def grayToBinary(self,n):

        ##Your code here

Solutions:
class Solution:

	def grayToBinary(self, n: int) -> int:
		binary = n
		while n > 0:
			n >>= 1
			binary ^= n
		return binary


class Solution:

	def grayToBinary(self, n):
		b = 0
		while n > 0:
			b = b ^ n
			n = n >> 1
		return b


class Solution:

	def grayToBinary(self, n):
		res = n
		while n > 0:
			n >>= 1
			res ^= n
		return res


class Solution:

	def grayToBinary(self, n):
		b = 0
		for i in iter(int, 1):
			if n > 0:
				b = b ^ n
				n = n >> 1
			else:
				break
		return b


class Solution:

	def grayToBinary(self, n: int) -> int:
		mask = n
		while mask != 0:
			mask >>= 1
			n ^= mask
		return n


class Solution:

	def grayToBinary(self, n):
		ans = n
		while n > 0:
			n = n >> 1
			ans = ans ^ n
		return ans


class Solution:

	def grayToBinary(self, n):
		gray_rep = bin(n)[2:]
		binary = gray_rep[0]
		for i in range(1, len(gray_rep)):
			if gray_rep[i] == '0':
				binary += binary[i - 1]
			else:
				binary += str(int(not int(binary[i - 1])))
		return int(binary, 2)


class Solution:

	def grayToBinary(self, n):
		c = 0
		while n:
			c ^= n
			n >>= 1
		return c


class Solution:

	def grayToBinary(self, n):
		binary = n & 1 << len(bin(n)) - 3
		for i in range(len(bin(n)) - 4, -1, -1):
			binary |= (binary >> 1 ^ n & 1 << i) & 1 << i
		return binary


import math

class Solution:

	def grayToBinary(self, n):
		t = n
		while t != 0:
			t = t >> 1
			n = n ^ t
		return n


class Solution:

	def grayToBinary(self, n):
		v = 0
		while n:
			v = v ^ n
			n = n >> 1
		return v


class Solution:

	def grayToBinary(self, n):
		a = n >> 1
		while a:
			n ^= a
			a = a >> 1
		return n


class Solution:

	def grayToBinary(self, n):
		k = len(bin(n)) - 2
		bin_y = 0
		xor = n >> k & 1
		bin_y = bin_y + xor
		k = k - 1
		while k >= 0:
			xor = xor ^ n >> k & 1
			bin_y = bin_y + (xor << k)
			k = k - 1
		return bin_y


class Solution:

	def grayToBinary(self, n):
		N = n
		while n > 0:
			n = n >> 1
			N = N ^ n
		return N


class Solution:

	def grayToBinary(self, n):
		x = 0
		while n > 0:
			x = x ^ n
			n = n >> 1
		return x


class Solution:

	def grayToBinary(self, n):
		temp = n
		while n > 0:
			n >>= 1
			temp = temp ^ n
		return temp


class Solution:

	def grayToBinary(self, n):
		inv = 0
		while n:
			inv = inv ^ n
			n = n >> 1
		return inv
		return n ^ n >> 1


class Solution:

	def grayToBinary(self, n):
		r = n
		n >>= 1
		while n:
			r ^= n
			n >>= 1
		return r


class Solution:

	def grayToBinary(self, n):
		result = n
		while n > 0:
			n = n >> 1
			result ^= n
		return result


class Solution:

	def grayToBinary(self, n):
		count = 0
		while n:
			count ^= n
			n >>= 1
		return count


class Solution:

	def grayToBinary(self, n):
		div = n
		while div // 2 != 0:
			n = n ^ div // 2
			div = div // 2
		return n


class Solution:

	def grayToBinary(self, n):
		x = n
		while True:
			if n == x ^ x >> 1:
				return x
				break
			x = x ^ x >> 1