Given a 32 bit number X, reverse its binary form and print the answer in decimal.
Example 1:
Input:
X = 1
Output:
2147483648 
Explanation:
Binary of 1 in 32 bits representation-
00000000000000000000000000000001
Reversing the binary form we get, 
10000000000000000000000000000000,
whose decimal value is 2147483648.
Example 2:
Input:
X = 5
Output:
2684354560 
Explanation:
Binary of 5 in 32 bits representation-
00000000000000000000000000000101
Reversing the binary form we get, 
10100000000000000000000000000000,
whose decimal value is 2684354560.
Your Task:
You don't need to read input or print anything. Your task is to complete the function reversedBits() which takes an Integer X as input and returns the answer.
Expected Time Complexity: O(log(X))
Expected Auxiliary Space: O(1)
Constraints:
0 <= X < 2^{32}

Difficulty: EASY
Tags: Bit manipulation

Data structures

Mathematics
Skill types: Bit manipulation

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

Starter code:
#User function Template for python3



class Solution:

    def reversedBits(self, X):

        # code here 

Solutions:
class Solution:

	def reversedBits(self, X):
		d = format(X, '032b')
		r = d[::-1]
		return int(r, 2)


class Solution:

	def reversedBits(self, x):
		p = 31
		ans = 0
		while x:
			if x & 1:
				ans += 2 ** p
			p -= 1
			x = x >> 1
		return ans


class Solution:

	def reversedBits(self, X):
		binary = []
		binary = format(X, '032b')
		reversed = str(binary)[::-1]
		return int(reversed, 2)


class Solution:

	def reversedBits(self, X):
		b = '{:032b}'.format(X)
		c = b[::-1]
		return int(c, 2)


class Solution:

	def reversedBits(self, X):
		binary_str = '{0:032b}'.format(X)[::-1]
		return int(binary_str, 2)


class Solution:

	def reversedBits(self, X):
		s1 = str(bin(X))[2:]
		s1 = str((32 - len(s1)) * '0') + s1
		s2 = int(s1[::-1], 2)
		return s2


class Solution:

	def reversedBits(self, X):
		s = ''
		while X != 0:
			r = X % 2
			s = s + str(r)
			X = X // 2
		re = (32 - len(s)) * '0' + s[::-1]
		sum = 0
		for i in range(len(re)):
			sum = sum + 2 ** i * int(re[i])
		return sum


class Solution:

	def reversedBits(self, X):
		X = str(bin(X)[2:].zfill(32))
		y = X[::-1]
		z = int(y, 2)
		return z


class Solution:

	def reversedBits(self, X):
		X = bin(X)[2:]
		N = '0' * (32 - len(X)) + X
		N = N[::-1]
		return int(N, 2)


class Solution:

	def reversedBits(self, x):
		reversed_bits = 0
		for i in range(32):
			reversed_bits = reversed_bits << 1
			reversed_bits |= x & 1
			x >>= 1
		return reversed_bits


class Solution:

	def reversedBits(self, X):
		return int(str(bin(X))[:1:-1] + '0' * (32 - len(str(bin(X))[:1:-1])), base=2)


class Solution:

	def reversedBits(self, X):
		n = X
		a = bin(n)
		at = str(a)
		at = at.replace('0b', '')
		tmp = len(at)
		at = at[::-1]
		while len(at) < 32:
			at += '0'
		ta = int(at, 2)
		return ta


class Solution:

	def reversedBits(self, X):
		s = ''
		while X:
			s = str(X % 2) + s
			X = X // 2
		s = '0' * (32 - len(s)) + s
		s = s[::-1]
		res = 0
		for i in range(32):
			if s[i] == '1':
				res = res + 2 ** (31 - i)
		return res


class Solution:

	def reversedBits(self, X):
		s = ''
		while X:
			s = str(X % 2) + s
			X = X // 2
		s = '0' * (32 - len(s)) + s
		s = s[::-1]
		return int(s, 2)


class Solution:

	def reversedBits(self, X):
		a = '{:032b}'.format(X)
		return int(a[::-1], 2)


class Solution:

	def reversedBits(self, X):
		c = '{:032b}'.format(X)
		c = str(c)
		x = c[::-1]
		a = int(x, 2)
		return a


class Solution:

	def reversedBits(self, X):
		binaryCode = format(X, '032b')
		reversebinaryCode = binaryCode[::-1]
		result = int(reversebinaryCode, 2)
		return result


class Solution:

	def reversedBits(self, X):
		list = [0] * 32
		i = 0
		rem = 0
		while X != 0:
			rem = X % 2
			list[31 - i] = rem
			X //= 2
			i += 1
		reversedArray = [0] * 32
		k = 31
		while k >= 0:
			reversedArray[31 - k] = list[k]
			k -= 1
		result = 0
		m = 0
		while m < 32:
			result += reversedArray[31 - m] * 2 ** m
			m += 1
		return result


class Solution:

	def reversedBits(self, X):
		binary = '{0:032b}'.format(int(X))
		b = ''
		for i in range(-1, -len(binary) - 1, -1):
			b += binary[i]
		return int(b, 2)


import math

class Solution:

	def getBits(self, n):
		bits = ''
		for _ in range(32):
			bits += str(n % 2)
			n = n // 2
		return bits

	def reversedBits(self, X):
		bits = self.getBits(X)
		res = 0
		i = 31
		for bit in bits:
			if int(bit):
				res += pow(2, i)
			i -= 1
		return res


class Solution:

	def reversedBits(self, X):
		a = str(bin(X).replace('0b', ''))
		a = [el for el in a]
		cnt = 32 - len(a)
		s = ['0'] * cnt + a
		s.reverse()
		return int(''.join(s), 2)


class Solution:

	def reversedBits(self, X):
		n = '{:032b}'.format(X)
		m = n[::-1]
		return int(m, 2)


class Solution:

	def reversedBits(self, X):
		d = ''
		while X:
			d = str(X % 2) + d
			X //= 2
		d = '0' * (32 - len(d)) + d
		d = d[::-1]
		i = len(d) - 1
		power = 0
		n = 0
		while i >= 0:
			n = n + int(d[i]) * 2 ** power
			power += 1
			i -= 1
		return n


class Solution:

	def reversedBits(self, X):
		binary = bin(X)[2:].zfill(32)
		revbinary = binary[::-1]
		integer = int(revbinary, 2)
		return integer


class Solution:

	def reversedBits(self, X):
		f = format(X, '032b')
		l = f[::-1]
		k = int(l, 2)
		return k


class Solution:

	def reversedBits(self, X):
		t = 31
		new = 0
		while t >= 0:
			y = X & 1
			if y:
				new = new | 1 << t
			else:
				new = new & ~(1 << t)
			t -= 1
			X = X >> 1
		return new


class Solution:

	def reversedBits(self, X):
		binstr = bin(X)
		binstr = binstr[2:]
		binstr = '0' * (32 - int(len(binstr))) + binstr
		revbinstr = binstr[::-1]
		op = int(revbinstr, 2)
		return op


class Solution:

	def reversedBits(self, X):
		result = 0
		for i in range(32):
			result <<= 1
			result |= X & 1
			X >>= 1
		return result


class Solution:

	def reversedBits(self, X):
		n = X
		i = 31
		start = 0
		ans = 0
		while i >= 0:
			div = n % 2
			ans += div * pow(2, i)
			n = n // 2
			i -= 1
			start += 1
		return ans


class Solution:

	def reversedBits(self, X):
		b = bin(X)[2:].zfill(32)
		r = b[::-1]
		i = int(r, 2)
		return i


class Solution:

	def reversedBits(self, X):
		binary = bin(X)[2:].zfill(32)
		reversed_binary = binary[::-1]
		decimal = int(reversed_binary, 2)
		return decimal


class Solution:

	def reversedBits(self, X):
		return int(bin(X).replace('0b', '').zfill(32)[::-1], 2)


class Solution:

	def reversedBits(self, X):
		binary = format(X, '032b')
		arr = list(binary)
		(s, e) = (0, len(arr) - 1)
		while s < e:
			(arr[s], arr[e]) = (arr[e], arr[s])
			s += 1
			e -= 1
		num = ''.join(arr)
		res = int(num, 2)
		return res


class Solution:

	def reversedBits(self, X):
		a = '{:032b}'.format(X)
		res = 0
		res = a[::-1]
		s = int(res, 2)
		return s


class Solution:

	def findkthbitset(self, k, A):
		if 1 << k - 1 & A != 0:
			return True
		return False

	def reversedBits(self, X):
		ans = 0
		for i in range(1, 33):
			if self.findkthbitset(i, X):
				ans = 1 << 32 - i | ans
		return ans


class Solution:

	def reversedBits(self, X):
		b = bin(X).replace('0b', '')
		b = '0' * (32 - len(b)) + b
		return int(b[::-1], 2)


class Solution:

	def reversedBits(self, X):
		st = 0
		cnt = 31
		while X:
			st = st + int(X % 2) * 2 ** cnt
			X = X // 2
			cnt -= 1
		return int(st)


class Solution:

	def reversedBits(self, X):
		ind = 31
		result = 0
		while X > 0:
			temp = X % 2
			if temp == 1:
				result = result + 2 ** ind
			ind = ind - 1
			X = int(X / 2)
		return result


class Solution:

	def reversedBits(self, X):
		x = bin(X)[2:]
		l = len(x)
		x = x[::-1] + (32 - l) * '0'
		return int(x, 2)


class Solution:

	def reversedBits(self, X):
		s = str(bin(X))[2:]
		rem = 32 - len(s)
		s = '0' * rem + s
		s = s[::-1]
		res = int(s, 2)
		return res


class Solution:

	def reversedBits(self, X):
		b = bin(X)[2:]
		c = str(b.zfill(32))
		d = c[::-1]
		return int(d, 2)


class Solution:

	def reversedBits(self, X):
		t = bin(X)[2:].zfill(32)
		t = str(t[::-1])
		m = int(t, 2)
		return m


class Solution:

	def reversedBits(self, n):
		n = X
		res = ['0'] * 32
		i = 0
		while n > 0:
			if n & 1 == 1:
				res[i] = '1'
			else:
				res[i] = '0'
			n = n >> 1
			i += 1
		res = ''.join(res)
		return int(res, 2)


class Solution:

	def reversedBits(self, X):
		count = 0
		temp = X
		ans = 0
		while temp > 0:
			bit = temp & 1
			ans = ans << 1 | bit
			count += 1
			temp = temp >> 1
		while count <= 31:
			ans = ans << 1
			count += 1
		return ans


class Solution:

	def reversedBits(self, X):
		h = bin(X)[2:]
		s = '0' * (32 - len(h)) + h
		return int(s[::-1], 2)


class Solution:

	def reversedBits(self, X):
		b = bin(X)[2:]
		if len(b) < 32:
			x = 32 - len(b)
			b = '0' * x + b
			b = b[::-1]
			return int(b, 2)
		else:
			b = b[::-1]
			return int(b, 2)


class Solution:

	def reversedBits(self, X):
		num = X
		ns = ''
		add = 0
		i = 31
		while X > 0:
			r = X % 2
			add += r * 2 ** i
			i -= 1
			X = X // 2
		return add


class Solution:

	def reversedBits(self, X):
		s1 = '{:032b}'.format(X)
		s1 = s1[::-1]
		s2 = int(s1, 2)
		return s2


class Solution:

	def reversedBits(self, X):
		binary = '{: 033b}'.format(X)
		rev = str(binary[::-1])
		return int(rev, 2)


class Solution:

	def reversedBits(self, X):
		sum_ = 0
		for binary_index in range(31, -1, -1):
			number = 2 ** binary_index
			if number <= X:
				X -= number
				sum_ += 2 ** (31 - binary_index)
		return sum_


class Solution:

	def reversedBits(self, X):
		l = [0] * 32
		ans = 0
		for i in range(len(l)):
			r = X % 2
			l[i] = r
			X = X // 2
		for i in range(len(l)):
			if l[i] == 1:
				ans += 2 ** (31 - i)
		return ans


class Solution:

	def reversedBits(self, x):
		b = bin(x)[2:]
		b = '0' * (32 - len(b)) + b
		b = b[::-1]
		return int(b, 2)


class Solution:

	def reversedBits(self, X):
		ans = '{:032b}'.format(X)
		ans = ans[::-1]
		base = 31
		res = 0
		for i in ans:
			res += 2 ** base * int(i)
			base -= 1
		return res


class Solution:

	def reversedBits(self, X):
		s = ''
		while X > 0:
			rem = X % 2
			s = str(rem) + s
			X = X // 2
		k = 32 - len(s)
		if k > 0:
			s = '0' * k + s
		val = 0
		for i in range(len(s)):
			val += 2 ** i * int(s[i])
		return val


class Solution:

	def reversedBits(self, X):
		binary = '{0:032b}'.format(X)
		res = 0
		for (i, n) in enumerate(binary):
			res += 2 ** i * int(n)
		return res


class Solution:

	def reversedBits(self, X):
		binary_str = bin(X)[2:]
		binary_str = binary_str.zfill(32)
		reversed_binary_str = binary_str[::-1]
		reversed_int = int(reversed_binary_str, 2)
		return reversed_int


class Solution:

	def reversedBits(self, X):
		binaryForm = bin(X)[2:]
		curr = 31
		res = 0
		for i in binaryForm[::-1]:
			if i == '1':
				res += 2 ** curr
			curr -= 1
		return res


class Solution:

	def reversedBits(self, X):
		binary_str = '{0:b}'.format(X)
		binary_str = binary_str.zfill(32)
		reversed_str = binary_str[::-1]
		return int(reversed_str, 2)


class Solution:

	def reversedBits(self, X):
		ans = 0
		for i in range(32):
			ans = ans * 2 + (X & 1)
			X = X >> 1
		return ans


class Solution:

	def reversedBits(self, X):
		res = 0
		for i in range(32):
			bit = X >> i & 1
			res = res | bit << 31 - i
		return res


class Solution:

	def reversedBits(self, n):
		l = []
		while n > 0:
			l.append(n % 2)
			n = n // 2
		reversed(l)
		p = len(l)
		s = 0
		for i in range(0, p):
			t = pow(2, 31 - i)
			s = s + l[i] * t
		return s


class Solution:

	def reversedBits(self, X):
		b = bin(X)[2:]
		if len(b) > 31:
			return X
		res = 0
		for i in range(32):
			bit = X >> i & 1
			res |= bit << 31 - i
		return res


import decimal

class Solution:

	def reversedBits(self, X):
		a = bin(X)[2:].zfill(32)
		k = a[::-1]
		b = int(k, 2)
		return b
		binary = bin(X)[2:].zfill(32)


class Solution:

	def reverse(self, X):
		fp = 0
		rp = len(X) - 1
		while fp < rp:
			(X[fp], X[rp]) = (X[rp], X[fp])
			fp += 1
			rp -= 1

	def getBits(self, X):
		bits = list(bin(X)[2:])
		return bits

	def getDecimal(self, bits):
		zerosToAppend = 32 - len(bits)
		for i in range(zerosToAppend):
			bits.append('0')
		bitString = ''.join(bits)
		return int(bitString, 2)

	def reversedBits(self, X):
		bits = self.getBits(X)
		self.reverse(bits)
		return self.getDecimal(bits)