Created
September 7, 2021 15:02
-
-
Save codecakes/9f905eb5914f031b65c6bb9ab9b39153 to your computer and use it in GitHub Desktop.
reverse integer bits in binary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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