Given a number, the task is to set all odd bits of a number.
NOTE: Position of Least Significant Bit is considered as 1.
Example 1:
Input: n = 20
Output: 21 
Explanation: Binary representation of 20 
is 10100. Setting all odd bits make the 
number 10101 which is binary
representation of 21.
Example 2:
Input: n = 10
Output: 15
Explanation: Binary representation of 10
is 1010. Setting all odd bits make the
number 1111 which is binary representation
of 15.
Your Task:  
You dont need to read input or print anything. Complete the function setAllOddBitsÃ¢â¬â¹() which takes n as input parameter and returns the modified number.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Constraints:
1<= n <=10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Bit manipulation

Data structures

Starter code: #User function Template for python3
class Solution:
    def setAllOddBits (ob, n):
        # code here 

Solutions:
from math import ceil, log2

class Solution:

	def setAllOddBits(ob, n):
		for i in range(0, 32, 2):
			if 1 << i <= n:
				n |= 1 << i
		return n


class Solution:

	def setAllOddBits(ob, n):
		n = list(bin(n)[2:])
		for i in range(len(n) - 1, -1, -2):
			n[i] = '1'
		return int(''.join(n), 2)


class Solution:

	def setAllOddBits(ob, n):
		if n == 1:
			return 1
		num = bin(n)[2:]
		lenght = len(num) // 2
		item = int('01' * lenght, 2)
		return n | item


import math

class Solution:

	def setAllOddBits(ob, n):
		return ~(-1 << int(math.log(n) / math.log(2) + 1)) & (n | 1431655765)


class Solution:

	def setAllOddBits(ob, n):
		odd = n
		cnt = n
		i = 0
		while cnt > 0:
			if i % 2 == 0:
				odd = odd | 1 << i
			cnt = cnt // 2
			i += 1
		return odd


from math import log

class Solution:

	def setAllOddBits(ob, n):
		l = int(log(n, 2))
		if l & 1:
			l -= 1
		num = 1 << l
		for i in range(0, l + 1, 2):
			num = num | 1 << i
		return n | num


class Solution:

	def setAllOddBits(ob, n):
		k = bin(n)
		s = k[2:]
		t = ''
		s = s[::-1]
		for i in range(0, len(s)):
			if i % 2 == 0:
				t = t + '1'
			else:
				t = t + s[i]
		t = t[::-1]
		y = int(t, 2)
		return y


class Solution:

	def setAllOddBits(ob, n):
		x = bin(n)[2:]
		x = [i for i in x]
		x = x[::-1]
		for i in range(len(x)):
			if i % 2 == 0:
				x[i] = '1'
		x = x[::-1]
		return int(''.join(x), 2)


class Solution:

	def setAllOddBits(ob, n):
		N = bin(n)[2:][::-1]
		res = ''
		for i in range(len(N)):
			if i % 2 == 0:
				res += '1'
			else:
				res += N[i]
		return int(res[::-1], 2)


class Solution:

	def setAllOddBits(ob, n):
		binary = bin(n).replace('0b', '')[::-1]
		binary = list(binary)
		for i in range(0, len(binary)):
			if i % 2 == 0:
				if binary[i] == '0':
					binary[i] = '1'
		ans = ''.join(binary[::-1])
		return int(ans, 2)


class Solution:

	def setAllOddBits(ob, n):
		a = bin(n).replace('0b', '')
		r = ''
		if len(a) % 2 == 0:
			for i in range(len(a) - 1, -1, -1):
				if i % 2 == 1:
					r = '1' + r
				else:
					r = a[i] + r
		else:
			for i in range(len(a) - 1, -1, -1):
				if i % 2 == 0:
					r = '1' + r
				else:
					r = a[i] + r
		return int(r, 2)


class Solution:

	def setAllOddBits(ob, n):
		S = list(bin(n)[2:])
		length = len(S)
		for i in range(length - 1, -1, -2):
			if S[i] == '0':
				S[i] = '1'
		return int(''.join(S), 2)


class Solution:

	def setAllOddBits(ob, n):
		binary = '{0:b}'.format(n)
		ans = ''
		if len(binary) % 2 == 0:
			for i in range(len(binary)):
				if i % 2 != 0:
					ans += str(1 | int(binary[i]))
				else:
					ans += binary[i]
			return int(ans, 2)
		else:
			for i in range(len(binary)):
				if i % 2 == 0:
					ans += str(1 | int(binary[i]))
				else:
					ans += binary[i]
			return int(ans, 2)


class Solution:

	def setAllOddBits(ob, n):
		n = list(bin(n)[2:])
		j = -1
		while j != -len(n):
			if j % 2:
				n[j] = '1'
			j -= 1
		return eval('0b' + ''.join(n))


class Solution:

	def setAllOddBits(ob, n):
		count = 0
		res = 0
		temp = n
		while temp > 0:
			if count % 2 == 0:
				res |= 1 << count
			count += 1
			temp >>= 1
		return n | res


class Solution:

	def setAllOddBits(ob, n):
		x = bin(n)
		x = x[:1:-1]
		p = ''
		for i in range(len(x)):
			if i % 2 != 0:
				p = p + x[i]
			else:
				p = p + '1'
		p = p[::-1]
		return int(p, 2)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.setAllOddBits(n))


class Solution:

	def setAllOddBits(ob, n):
		s = bin(n)
		s = s[2:]
		d = len(s)
		if d % 2 == 1:
			c = 0
		else:
			c = 1
		t = ''
		for i in range(d):
			if i % 2 == c:
				t += '1'
			else:
				t += s[i]
		d = d - 1
		s = 0
		for i in t:
			if i == '1':
				s += 2 ** d
			d = d - 1
		return s
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.setAllOddBits(n))


class Solution:

	def setAllOddBits(ob, n):
		result = 0
		index = 1
		while n:
			currentBit = n & 1
			if index & 1:
				currentBit = 1
			mask = currentBit << index - 1
			result = result | mask
			n = n >> 1
			index += 1
		return result


import math

class Solution:

	def setAllOddBits(ob, n):
		bits = math.floor(math.log(n, 2)) + 1
		bits = math.ceil(bits / 2)
		num = int((4 ** bits - 1) * 1 / 3)
		return n | num


import math

class Solution:

	def setAllOddBits(ob, n):
		res = n
		for i in range(0, int(math.log(n, 2)) + 1, 2):
			res = res | 1 << i
		return res


import math

class Solution:

	def setAllOddBits(ob, n):
		bits = int(math.log(n, 2)) + 1
		odd_bits = 366503875925
		fact = odd_bits & 2 ** bits - 1
		return n | fact


class Solution:

	def setAllOddBits(self, n):
		mask = 0
		for i in range(n.bit_length(), 1, -1):
			if i % 2 == 0:
				mask = (mask << 1) + 1
			else:
				mask <<= 1
		return n | mask


class Solution:

	def setAllOddBits(ob, n):
		temp = n
		mask = 1
		while temp > 0:
			n |= mask
			mask = mask << 2
			temp = temp >> 2
		return n