Problem :

https://leetcode.com/problems/reverse-bits/


My Solution :

class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
ans = 0
for _ in range(32):
ans = ans*2 + n%2
n //= 2
return ans


Comment :

위는 산술연산 아래는 비트연산


My Solution2 :

class Solution:
# @param n, an integer
# @return an integer
def reverseBits(self, n):
ans = 0
for _ in range(32):
ans = (ans << 1) + (n & 1)
n >>= 1
return ans