[Bit operation-simple] 190. Reverse binary digits (one line of code, add bit operation solution tomorrow)

[Title]
Reverse the binary bits of a given 32-bit unsigned integer.
[Example 1]
Input: 00000010100101000001111010011100
Output: 00111001011110000010100101000000
Explanation: The input binary string 00000010100101000001111010011100 represents an unsigned integer 43261596,
so it returns 964176192, and its binary representation is 00111001011110000010100101000000.
[Example 2]
Input: 11111111111111111111111111111111
Output: 10111111111111111111111111111111
Explanation: The input binary string 11111111111111111111111111111101 represents an unsigned integer 4294967293, so 3221225471 is returned, and its binary representation is 10111111111111111111111111111
[Hint]
Please note that in some languages ​​(such as Java) there is no Signed integer type. In this case, both input and output will be designated as signed integer types, and should not affect your implementation, because the internal binary representation is the same regardless of whether the integer is signed or unsigned.
In Java, the compiler uses two's complement notation to represent signed integers. Therefore, in Example 2 above, the input represents a signed integer -3, and the output represents a signed integer -1073741825.
[Code]
[Python]
[One line of code]
Insert picture description here

class Solution:
    def reverseBits(self, n: int) -> int:
        return int(("{0:0>32}".format(bin(n)[2:]))[::-1],2)

Guess you like

Origin blog.csdn.net/kz_java/article/details/115101151