Reverse the order of the 8 bits in a byte

Quick Overview

Reverse the order of the 8 bits in an unsigned byte and return the new integer value. The problem tests bit extraction with masks and shifts, keeping leading zeros in a fixed-width value, and reasoning about where each bit ends up after the reversal.

Reverse the order of the 8 bits in a byte

Company: Apple

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

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 ```python 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 - `0 <= n <= 255` ### Examples ```text Input: n = 210 Output: 75 ``` 210 in binary is `11010010`. Reversed, it reads `01001011`, which is 64 + 8 + 2 + 1 = 75. ```text 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. ```text 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.

Overview: Reverse the order of the 8 bits in an unsigned byte and return the new integer value. The problem tests bit extraction with masks and shifts, keeping leading zeros in a fixed-width value, and reasoning about where each bit ends up after the reversal.

Apple logo
Apple
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

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

  • 0 <= n <= 255

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.