Given an integer n representing an unsigned 8-bit byte (0 to 255), reverse the order of its 8 bits and return the resulting integer.
For example, the byte 0b11010010 (210 in decimal) reversed becomes 0b01001011 (75 in decimal).
The input is guaranteed to fit within an unsigned 8-bit integer.
Function Signature
def reverse_byte(n: int) -> int:
Rules
-
Treat
n
as exactly 8 bits, including any leading zeros. Bit 0 (the least significant bit) moves to bit 7, bit 1 moves to bit 6, and so on up to bit 7, which moves to bit 0.
Constraints
Examples
Input: n = 210
Output: 75
210 in binary is 11010010. Reversed, it reads 01001011, which is 64 + 8 + 2 + 1 = 75.
Input: n = 1
Output: 128
As a full byte, 1 is 00000001. Its only set bit moves from position 0 to position 7, giving 10000000 = 128.
Input: n = 6
Output: 96
6 is 00000110. The set bits at positions 1 and 2 move to positions 6 and 5, giving 01100000 = 64 + 32 = 96.