Skip to content

Instantly share code, notes, and snippets.

@codecakes
Created September 7, 2021 15:02
Show Gist options
  • Save codecakes/9f905eb5914f031b65c6bb9ab9b39153 to your computer and use it in GitHub Desktop.
Save codecakes/9f905eb5914f031b65c6bb9ab9b39153 to your computer and use it in GitHub Desktop.
reverse integer bits in binary
import math
class Solution:
def reverseBits(self, bin_int):
'''
:type bin_int: int
:rtype: int
'''
if bin_int <= 1: return bin_int
bit_size = bin_int.bit_length()
start = 1
res = None
lsb = None
for each_shift in range(bit_size):
# print(res, bin_int)
lsb = (bin_int & 1)
if res == None:
res = lsb
else:
res <<= 1
res |= lsb
bin_int >>= 1
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment