""" 0190.1 - Reverse Bits - Solution 1 - Bit Manipulation (Iterative) """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def reverseBits(self, n: int) -> int:
"""Reverse Bits Function"""
result = 0
for i in range(32):
result = (result << 1) + (n & 1)
n >>= 1
return result
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().reverseBits(43261596)) # 964176192
print(Solution().reverseBits(4294967293)) # 3221225471
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0190.2 - Reverse Bits - Solution 2 - Bit Manipulation (OR with Position Shifting) """
#####################################################################################
# Imports
#####################################################################################
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def reverseBits(self, n: int) -> int:
"""Reverse Bits Function"""
result = 0
for i in range(32):
bit = (n >> i) & 1
result |= bit << (31 - i)
return result
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().reverseBits(43261596)) # 964176192
print(Solution().reverseBits(4294967293)) # 3221225471
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()