Given a number N, the task is to find XOR of count of 0s and count of 1s in binary representation of a given number.
 
Example 1:
Input: N = 5
Output: 3
Explanation: 
Binary representation: 101
Set_bit_count: 2
Unset_bit_count: 1
2 XOR 1 = 3
Example 2:
Input: N = 7
Output: 3
Expalanation:
Binary representation: 111
Set_bit_count: 3
Unset_bit_count: 0
3 XOR 0 = 3
Your Task:
You don't need to read or print anything. Your task is to complete the function find_xor() which takes N as input parameter and returns the XOR of count of 0s and count of 1s in binary representation of N.
Expected Time Complexity: O(log(N))
Expected Space Complexity: O(1)
Constranits:
1 <= N <= 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3

class Solution:
	def find_xor(self, n):
		# Code here

Solutions:
class Solution:

	def find_xor(self, n):
		x = bin(n)[2:]
		o = x.count('1')
		z = x.count('0')
		return o ^ z


class Solution:

	def find_xor(self, n):
		s = bin(n)[2:]
		return s.count('1') ^ s.count('0')


class Solution:

	def find_xor(self, n):
		s = ''
		s = bin(n).replace('0b', ' ')
		k1 = s.count('1')
		k2 = s.count('0')
		return k1 ^ k2


class Solution:

	def find_xor(self, n):
		x = bin(n)[2:]
		c = x.count('1')
		k = x.count('0')
		return c ^ k


class Solution:

	def find_xor(self, n):
		s1 = 0
		s2 = 0
		ans = format(n, 'b')
		for i in ans:
			if i == '1':
				s1 = s1 + 1
			else:
				s2 = s2 + 1
		return s1 ^ s2


class Solution:

	def find_xor(self, n):
		s = bin(n)
		one = 0
		zero = 0
		for i in range(2, len(s)):
			if s[i] == '0':
				zero += 1
			else:
				one += 1
		return one ^ zero


class Solution:

	def find_xor(self, n):
		num = bin(n)[2:]
		one = 0
		zero = 0
		for item in num:
			if item == '1':
				one += 1
			else:
				zero += 1
		return one ^ zero


class Solution:

	def find_xor(self, n):
		s = bin(n)[2:]
		sc = 0
		nc = 0
		for i in s:
			if i == '1':
				sc += 1
			elif i == '0':
				nc += 1
		return sc ^ nc


class Solution:

	def find_xor(self, n):
		count0 = 0
		count1 = 0
		i = 1
		while n > 0:
			if n & 1:
				count1 += 1
			else:
				count0 += 1
			n = n >> 1
		return count0 ^ count1


class Solution:

	def find_xor(self, n):
		a = bin(n)[2:]
		x = a.count('0')
		y = a.count('1')
		return x ^ y


import math

class Solution:

	def find_xor(self, n):
		c = 0
		k = int(math.log2(n)) + 1
		while n > 0:
			c = c + 1
			n = n & n - 1
		return c ^ k - c


class Solution:

	def find_xor(self, n):
		count0 = 0
		count1 = 0
		while n:
			count1 += n & 1
			count0 += not n & 1
			n = n >> 1
		return count0 ^ count1


class Solution:

	def find_xor(self, n):
		count = [0, 0]
		while n:
			count[n & 1] += 1
			n >>= 1
		(x, y) = count
		return x ^ y


class Solution:

	def find_xor(self, n):
		count1 = 0
		count2 = 0
		while n != 0:
			if n & 1 == 1:
				count1 += 1
			else:
				count2 += 1
			n = n >> 1
		return count1 ^ count2


class Solution:

	def find_xor(self, n):
		x = bin(n)[2:]
		one = 0
		zero = 0
		for i in x:
			if i == '0':
				zero += 1
			else:
				one += 1
		return one ^ zero


class Solution:

	def find_xor(self, n):
		s = str(bin(n))
		setb = 0
		unsetb = 0
		for i in range(2, len(s)):
			if s[i] == '1':
				setb += 1
			else:
				unsetb += 1
		return int(setb) ^ int(unsetb)


class Solution:

	def find_xor(self, n):
		bi = bin(n).replace('0b', '')
		o = bi.count('1')
		z = bi.count('0')
		return o ^ z


class Solution:

	def find_xor(self, n):
		return bin(n).count('0') - 1 ^ bin(n).count('1')


class Solution:

	def find_xor(self, n):
		setcount = 0
		unsetcount = 0
		while n != 0:
			digit = n % 2
			n = n // 2
			if digit == 1:
				setcount += 1
			else:
				unsetcount += 1
		return setcount ^ unsetcount


class Solution:

	def find_xor(self, n):
		m = bin(n).replace('0b', '')
		o = m.count('1')
		p = m.count('0')
		return o ^ p


class Solution:

	def find_xor(self, n):
		h = bin(n)[2:]
		(c, m) = (0, 0)
		for i in h:
			if i == '1':
				c += 1
			else:
				m += 1
		return c ^ m


class Solution:

	def find_xor(self, n):
		set = 0
		reset = 0
		while n > 0:
			if n & 1 == 1:
				set = set + 1
			else:
				reset = reset + 1
			n = n >> 1
		return set ^ reset


class Solution:

	def find_xor(self, n):
		zeros = 0
		ones = 0
		while n > 0:
			if n & 1 == 1:
				ones += 1
			else:
				zeros += 1
			n = n >> 1
		return zeros ^ ones


class Solution:

	def find_xor(self, n):
		s = bin(n).replace('0b', '')
		x = s.count('1')
		y = s.count('0')
		return x ^ y


class Solution:

	def find_xor(self, n):
		b = bin(n)[2:]
		c1 = b.count('1')
		c = b.count('0')
		return c1 ^ c


class Solution:

	def find_xor(self, n):
		(a, b) = (0, 0)
		while n:
			if n & 1:
				a += 1
			else:
				b += 1
			n = n >> 1
		return a ^ b


class Solution:

	def find_xor(self, n):
		count_zero = 0
		count_one = 0
		while n != 0:
			bit = n & 1
			if bit == 0:
				count_zero += 1
			else:
				count_one += 1
			n = n >> 1
		return count_one ^ count_zero


class Solution:

	def find_xor(self, n):
		x = list(bin(n)[2:])
		c0 = 0
		c1 = 0
		for i in x:
			if i == '0':
				c0 += 1
			else:
				c1 += 1
		return c1 ^ c0


class Solution:

	def find_xor(self, N):
		N = bin(N)
		N = list(N[2:])
		one = N.count('1')
		zero = N.count('0')
		return one ^ zero


class Solution:

	def find_xor(self, n):
		bits = 0
		temp = n
		while temp:
			bits = bits + 1
			temp = temp >> 1
		setbits = 0
		while n:
			setbits += 1
			n = n & n - 1
		return setbits ^ bits - setbits


class Solution:

	def find_xor(self, n):
		bit = bin(n)[2:]
		st = str(bit)
		count = 0
		for c in st:
			if c == '1':
				count = count + 1
		return count ^ len(st) - count
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.find_xor(n)
		print(ans)


class Solution:

	def find_xor(self, n):
		n = bin(n)
		n = n[2:]
		x = n.count('0')
		r = n.count('1')
		return x ^ r
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.find_xor(n)
		print(ans)


class Solution:

	def find_xor(self, n):
		z = 0
		o = 0
		while n != 0:
			if n & 1 == 0:
				z += 1
			else:
				o += 1
			n = n >> 1
		return z ^ o
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.find_xor(n)
		print(ans)


class Solution:

	def find_xor(self, n):
		b = bin(n).replace('0b', '')
		s = str(b)
		l = len(b)
		s1 = b.count('1')
		s0 = l - s1
		return s1 ^ s0
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.find_xor(n)
		print(ans)


class Solution:

	def find_xor(self, n):
		bits = bin(n)
		zero = 0
		one = 0
		bits = bits[2:]
		zero += bits.count('0')
		one += bits.count('1')
		return zero ^ one
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.find_xor(n)
		print(ans)


class Solution:

	def find_xor(self, n):
		s = 0
		u = 0
		b = bin(n)
		b = b[2:]
		for i in b:
			if i == '1':
				s += 1
			else:
				u += 1
		return s ^ u
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.find_xor(n)
		print(ans)


class Solution:

	def find_xor(self, n):
		c = bin(n)
		c = str(c)
		c = c.replace('0b', '')
		a = c.count('1')
		b = c.count('0')
		return a ^ b
if __name__ == '__main__':
	T = int(input())
	for i in range(T):
		n = int(input())
		ob = Solution()
		ans = ob.find_xor(n)
		print(ans)


class Solution:

	def find_xor(self, n):
		binary = bin(n)[2:]
		b = str(binary)
		ones = b.count('1')
		zeros = b.count('0')
		return ones ^ zeros


class Solution:

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


class Solution:

	def find_xor(self, n):
		a = int(bin(n)[2:])
		se = 0
		unse = 0
		for i in str(a):
			if i == '1':
				se += 1
			else:
				unse += 1
		return se ^ unse


class Solution:

	def find_xor(self, n):
		x = bin(n)[2:]
		one = x.count('1')
		zero = x.count('0')
		return one ^ zero


class Solution:

	def find_xor(self, n):
		binary = [int(b) for b in bin(n)[2:]]
		set_bit_count = binary.count(1)
		unset_bit_count = binary.count(0)
		return set_bit_count ^ unset_bit_count


class Solution:

	def find_xor(self, n):
		x = bin(n).replace('0b', '')
		z = str(x).count('0')
		o = str(x).count('1')
		s = z ^ o
		return s


class Solution:

	def find_xor(self, n):
		cnt1 = 0
		cnt2 = 0
		while n != 0:
			if n & 1 == 1:
				cnt1 += 1
			else:
				cnt2 += 1
			n = n >> 1
		return cnt1 ^ cnt2


import math

class Solution:

	def find_xor(self, n):
		bits = math.floor(math.log(n, 2)) + 1
		count = 0
		while n != 0:
			count += 1
			n = n & n - 1
		return count ^ bits - count


class Solution:

	def find_xor(self, n):
		i = 1
		c = 0
		d = 0
		while i <= n:
			if n & i:
				c += 1
			else:
				d += 1
			i = i << 1
		return c ^ d


class Solution:

	def find_xor(self, n):
		k = bin(n).replace('0b', '')
		a = k.count('0')
		b = k.count('1')
		return a ^ b


class Solution:

	def find_xor(self, n):
		setb = 0
		unset = 0
		st = bin(n)[2:]
		for i in range(len(st)):
			if st[i] == '1':
				setb += 1
			if st[i] == '0':
				unset += 1
		return setb ^ unset


class Solution:

	def find_xor(self, n):
		a = bin(n).replace('0b', '')
		st = a.count('1')
		unst = a.count('0')
		return st ^ unst


class Solution:

	def find_xor(self, n):
		j = str(bin(n).replace('0b', ''))
		i = j.count('0')
		k = j.count('1')
		return i ^ k


class Solution:

	def find_xor(self, n):
		t = bin(n)[2:]
		t1 = t.count('0')
		t2 = t.count('1')
		t3 = t1 ^ t2
		return t3


class Solution:

	def find_xor(self, n):
		o = 0
		e = 0
		while n:
			if n % 2 == 1:
				e += 1
			else:
				o += 1
			n //= 2
		return e ^ o


class Solution:

	def find_xor(self, n):
		x = bin(n).replace('0b', '')
		m = x.count('1')
		n = x.count('0')
		return m ^ n


class Solution:

	def find_xor(self, n):
		b = bin(n)[2:]
		z = 0
		o = 0
		for i in b:
			if i == '1':
				o += 1
			else:
				z += 1
		return z ^ o


import math

class Solution:

	def find_xor(self, n):
		c = 1
		k = int(math.log(n, 2)) + 1
		while n & n - 1 != 0:
			n = n & n - 1
			c += 1
		return c ^ k - c


class Solution:

	def find_xor(self, n):
		c = []
		while n > 0:
			a = n % 2
			c.append(a)
			n = n // 2
		return c.count(1) ^ c.count(0)


class Solution:

	def find_xor(self, n):
		setbit = 0
		offset = 0
		while n > 0:
			if n % 2 == 1:
				setbit += 1
			else:
				offset += 1
			n //= 2
		return setbit ^ offset


class Solution:

	def find_xor(self, n):
		k = bin(n).replace('0b', '')
		s = k.count('1')
		u = k.count('0')
		return s ^ u


class Solution:

	def find_xor(self, n):
		k = bin(n).replace('0b', '')
		s = k.count('1')
		o = list(k)
		u = len(o) - s
		return s ^ u


class Solution:

	def find_xor(self, n):
		s = 0
		us = 0
		while n:
			if n & 1:
				s += 1
			else:
				us += 1
			n = n >> 1
		return s ^ us


class Solution:

	def find_xor(self, n):
		s = str()
		while n != 0:
			rem = n % 2
			s = str(rem) + s
			n = n // 2
		sett = s.count('1')
		unset = s.count('0')
		return str(sett ^ unset)


class Solution:

	def find_xor(self, n):
		b = bin(n)[2:]
		set = b.count('1')
		unset = b.count('0')
		return set ^ unset


class Solution:

	def find_xor(self, n):
		bn = bin(n)
		b = str(bn)
		v1 = bn[2:]
		c0 = 0
		c1 = 0
		for i in v1:
			if i == '1':
				c1 += 1
			else:
				c0 += 1
		return c1 ^ c0


class Solution:

	def cout(self, n):
		(i, zr, on) = (0, 0, 0)
		while 1 << i <= n:
			if 1 << i & n:
				on += 1
			else:
				zr += 1
			i += 1
		return zr ^ on

	def find_xor(self, n):
		return self.cout(n)