Given an unsigned integer N. The task is to swap all odd bits with even bits. For example, if the given number is 23 (00010111), it should be converted to 43(00101011). Here, every even position bit is swapped with an adjacent bit on the right side(even position bits are highlighted in the binary representation of 23), and every odd position bit is swapped with an adjacent on the left side.
Example 1:
Input: N = 23
Output: 43
Explanation: 
Binary representation of the given number 
is 00010111 after swapping 
00101011 = 43 in decimal.
Example 2:
Input: N = 2
Output: 1
Explanation: 
Binary representation of the given number 
is 10 after swapping 01 = 1 in decimal.
Your Task: Your task is to complete the function swapBits() which takes an integer and returns an integer with all the odd and even bits swapped.
Expected Time Complexity: O(1).
Expected Auxiliary Space: O(1).
Constraints:
1 ≤ N ≤ 10^{9}

Difficulty: EASY
Tags: Bit manipulation

Data structures
Skill types: Bit manipulation

Data structures
Expected Time Complexity: O(1).
Expected Auxiliary Space: O(1).

Starter code:
#User function Template for python3



class Solution:

    

    ##Complete this function

    #Function to swap odd and even bits.

    def swapBits(self,n):

        #Your code here

Solutions:
class Solution:

	def swapBits(self, n):
		ans = 0
		i = 0
		while i < 31:
			if n & 1:
				ans += 2 ** (i + 1)
			if n >> 1 & 1:
				ans += 2 ** i
			i += 2
			n = n >> 2
		return ans


class Solution:

	def swapBits(self, n):
		evenBits = n & 2863311530
		oddBits = n & 1431655765
		evenBits = evenBits >> 1
		oddBits = oddBits << 1
		result = evenBits | oddBits
		return result


class Solution:

	def swapBits(self, N):
		even_mask = 2863311530
		odd_mask = 1431655765
		even_bits = (N & even_mask) >> 1
		odd_bits = (N & odd_mask) << 1
		return even_bits | odd_bits


class Solution:

	def swapBits(self, n):
		even_bits = n & 2863311530
		odd_bits = n & 1431655765
		even_bits >>= 1
		odd_bits <<= 1
		result = even_bits | odd_bits
		return result


class Solution:

	def swapBits(self, n):
		t = bin(n).replace('0b', '')
		t = list(t)
		if len(t) % 2 == 1:
			y = ['0']
			y.extend(t)
		else:
			y = t
		for i in range(0, len(y), 2):
			(y[i], y[i + 1]) = (y[i + 1], y[i])
		h = ''.join(y)
		return int(h, 2)


class Solution:

	def swapBits(self, n):
		b = bin(n)
		l = list(b[2:])
		k = len(l)
		if k % 2 != 0:
			l = ['0'] + l
			k = k + 1
		for i in range(0, k, 2):
			(l[i], l[i + 1]) = (l[i + 1], l[i])
		return int(''.join(l), 2)


class Solution:

	def swapBits(self, n):
		even = (n & 2863311530) >> 1
		odd = (n & 1431655765) << 1
		x = even | odd
		return x


class Solution:

	def swapBits(self, n):
		tmp = 2863311530
		return (n & tmp) >> 1 | (n & tmp >> 1) << 1


class Solution:

	def swapBits(self, n):
		tmp = 178956970
		return (n & tmp) >> 1 | (n & tmp >> 1) << 1


class Solution:

	def swapBits(self, n):
		odd = 89478485
		even = 178956970
		od = odd & n
		ev = even & n
		od = od << 1
		ev = ev >> 1
		h = od | ev
		return h


class Solution:

	def swapBits(self, n):
		st = bin(n)[2:]
		st = len(st) % 2 * '0' + st
		ans = ''
		i = 0
		while i < len(st):
			if len(st) - i < 2:
				ans += st[i]
			else:
				ans += st[i + 1] + st[i]
			i += 2
		return int(ans, 2)


class Solution:

	def swapBits(self, n):
		e = list(bin(n)[2:])
		c = len(e) % 8
		d = len(e) // 8
		if c > 0:
			b = ['0'] * (8 - c)
			b.extend(e)
		else:
			b = e
		for i in range(0, len(b) - 1, 2):
			(b[i], b[i + 1]) = (b[i + 1], b[i])
		a = ''.join(b)
		return int(a, 2)


class Solution:

	def swapBits(self, n):
		even_bits = n & 2863311530
		odd_bits = n & 1431655765
		even = even_bits >> 1
		odd = odd_bits << 1
		return even | odd


class Solution:

	def swapBits(self, n):
		c = n
		s = ''
		if c <= 0:
			return n
		while c > 0:
			k = str(c & 1)
			l = c >> 1
			k1 = str(l & 1)
			s = k + k1 + s
			c = l >> 1
		return int(s, 2)


class Solution:

	def swapBits(self, x):
		even_bits = x & 2863311530
		odd_bits = x & 1431655765
		even_bits >>= 1
		odd_bits <<= 1
		return even_bits | odd_bits


class Solution:

	def swapBits(self, n):
		evenBit = 2863311530
		oddBit = 1431655765
		ev = n & evenBit
		od = n & oddBit
		ev >>= 1
		od <<= 1
		return ev | od


class Solution:

	def swapBits(self, n):
		e = n & 2863311530
		o = n & 1431655765
		e >>= 1
		o <<= 1
		n = o | e
		return n


class Solution:

	def swapBits(self, n):
		x = 2863311530
		m = 1431655765
		om = n & x
		on = n & m
		om >>= 1
		on <<= 1
		c = om | on
		return c


class Solution:

	def swapBits(self, n):
		return (n & 2863311530) >> 1 | (n & 1431655765) << 1


class Solution:

	def swapBits(self, n):
		ebits = 2863311530 & n
		obits = 1431655765 & n
		ebits = ebits >> 1
		obits = obits << 1
		return ebits | obits


class Solution:

	def swapBits(self, n):
		x = bin(n)
		x = x[2:]
		y = 32 - len(x)
		z = '0' * y
		x = z + x
		x = list(x)
		for i in range(0, len(x) - 1, 2):
			(x[i], x[i + 1]) = (x[i + 1], x[i])
		x = ''.join(x)
		return int(x, 2)


class Solution:

	def swapBits(self, n):
		b = bin(n)
		b = b[2:]
		if len(b) % 2 == 1:
			b = '0' + b
		s = ''
		for i in range(0, len(b), 2):
			s += b[i + 1]
			s += b[i]
		return int(s, 2)


class Solution:

	def swapBits(self, n):
		mask = 0
		for i in range(0, 32, 2):
			mask += 1 << i
		odd_bits = n & mask
		even_bits = n & mask << 1
		return (odd_bits << 1) + (even_bits >> 1)


class Solution:

	def swapBits(self, n):
		xb = bin(n).replace('0b', '')
		if len(xb) % 2 != 0:
			xb = '0' + xb
		x = list(xb)
		for i in range(1, len(x), 2):
			temp = x[i - 1]
			x[i - 1] = x[i]
			x[i] = temp
		y = ''.join(x)
		return int(y, 2)


class Solution:

	def swapBits(self, n):
		odd_bits = n & 2863311530
		even_bits = n & 1431655765
		shifted_odd = odd_bits >> 1
		shifted_even = even_bits << 1
		output = shifted_odd | shifted_even
		return output


class Solution:

	def swapBits(self, n):
		a = format(n, 'b')
		if len(a) % 2 != 0:
			a = '0' + a
		a = [*a]
		for i in range(0, len(a) - 1, 2):
			(a[i], a[i + 1]) = (a[i + 1], a[i])
		a = ''.join(a)
		return int(a, 2)


class Solution:

	def swapBits(self, n):
		ans = 0
		for i in range(0, 31, 2):
			x = n >> i & 1
			y = n >> i + 1 & 1
			ans = ans | y << i
			ans = ans | x << i + 1
		return ans


class Solution:

	def swapBits(self, n):
		evenMask = 699050
		oddMask = 349525
		evenbits = n & evenMask
		oddbits = n & oddMask
		evenbits >>= 1
		oddbits <<= 1
		return evenbits | oddbits


class Solution:

	def swapBits(self, n):
		evens = n & 2863311530
		odds = n & 1431655765
		return evens >> 1 | odds << 1


class Solution:

	def swapBits(self, n):
		eve = n & 2863311530
		odd = n & 1431655765
		eve = eve >> 1
		odd = odd << 1
		return eve | odd


class Solution:

	def swapBits(self, n):
		even_bit = n & 2863311530
		odd_bit = n & 1431655765
		even_bit >>= 1
		odd_bit <<= 1
		return even_bit | odd_bit


import math

class Solution:

	def swapBits(self, n):
		size = int(math.log2(n)) + 1
		alternate_ones = 4 ** (size + 1 // 2) // 3
		rightShift = n >> 1 & alternate_ones
		leftShift = n << 1 & alternate_ones << 1
		return rightShift + leftShift


class Solution:

	def swapBits(self, n):
		evenBits = 2863311530
		oddBits = 1431655765
		leftNo = n & evenBits
		rightNo = n & oddBits
		leftNo = leftNo >> 1
		rightNo = rightNo << 1
		return leftNo | rightNo


class Solution:

	def swapBits(self, n):
		eb = n & 2863311530
		ob = n & 1431655765
		eb >>= 1
		ob <<= 1
		return eb | ob


class Solution:

	def swapBits(self, n):
		res = 0
		i = 0
		while i < 32:
			if n & 1:
				res += 1 << i + 1
			if n >> 1 & 1:
				res += 1 << i
			n >>= 2
			i += 2
		return res


class Solution:

	def swapBits(self, n):
		bitN = bin(n)[2:]
		bitN = list('0' * (32 - len(bitN)) + bitN)
		for i in range(0, 32, 2):
			(bitN[i], bitN[i + 1]) = (bitN[i + 1], bitN[i])
		return int('0b' + ''.join(bitN), 2)


class Solution:

	def swapBits(self, n):
		binary = list(bin(n)[2:])
		if len(binary) % 2 == 1:
			binary.insert(0, '0')
		for i in range(0, len(binary), 2):
			(binary[i], binary[i + 1]) = (binary[i + 1], binary[i])
		result = int(''.join(binary), 2)
		return result


class Solution:

	def swapBits(self, n):
		num = bin(n)[2:]
		if len(num) % 2:
			num = '0' + num
		output = ''
		for i in range(0, len(num), 2):
			output += num[i + 1]
			output += num[i]
		return int(output, 2)


class Solution:

	def swapBits(self, n):
		res = 0
		for i in range(0, 31, 2):
			if n % 2:
				res += 1 << i + 1
			if (n >> 1) % 2:
				res += 1 << i
			n = n >> 2
		return res


class Solution:

	def swapBits(self, n):
		even_bit_mask = 2863311530
		odd_bit_mask = 1431655765
		preserve_even_bit = n & even_bit_mask
		preserve_odd_bit = n & odd_bit_mask
		return preserve_even_bit >> 1 | preserve_odd_bit << 1


class Solution:

	def swapBits(self, n):
		x = n & 1431655765
		y = n & 45812984490
		y = y >> 1
		x = x << 1
		return x | y


class Solution:

	def swapBits(self, n):
		x = n & 1431655765
		y = n & 2863311530
		y = y >> 1
		x = x << 1
		return x | y


class Solution:

	def swapBits(self, n):
		evenbits = n & 2863311530
		oddbits = n & 1431655765
		evenbits = evenbits >> 1
		oddbits = oddbits << 1
		return evenbits | oddbits


class Solution:

	def swapBits(self, n):
		e = 226854911280625642308916404954512140970
		o = 7433581732843541047178572757549453835326805
		odd = n & o
		even = n & e
		res = even >> 1 | odd << 1
		return res


class Solution:

	def swapBits(self, n):
		x = bin(n)
		l = list(x[2:])
		if len(l) % 2 != 0:
			l.insert(0, '0')
		for i in range(0, len(l) - 1, 2):
			(l[i], l[i + 1]) = (l[i + 1], l[i])
		str = ''.join(l)
		return int(str, 2)


class Solution:

	def swapBits(self, n):
		a = n << 1
		b = n >> 1
		a = a & 2863311530
		b = b & 1431655765
		return a | b


class Solution:

	def swapBits(self, n):
		odd = 357913941
		even = 715827882
		odd = n & odd
		even = n & even
		odd = odd << 1
		even = even >> 1
		return odd | even


class Solution:

	def swapBits(self, n):
		a = bin(n)[2:]
		l = len(a)
		a = '0' * (l % 2) + a
		l = len(a)
		b = a[0::2]
		c = a[1::2]
		d = ''
		(j, k) = (0, 0)
		for i in range(l):
			if i % 2 == 0:
				d += c[j]
				j += 1
			else:
				d += b[k]
				k += 1
		return int(d, 2)