basics · beginner · ~15 min

Reverse a byte

Reverse the 8 bits of a byte.

Challenge

Implement:

uint8_t reverse_byte(uint8_t b);

Return b with its 8 bits reversed.

Input format

A byte.

Output format

The bit-reversed byte.

Constraints

None.

Starter code

#include <stdint.h>
/* Reverse the 8 bits of a byte. */
uint8_t reverse_byte(uint8_t b){ return b; }

Common mistakes

Letting the intermediate value exceed 8 bits without masking back to uint8_t.

Edge cases to handle

0xA5 reverses to 0xA5 (palindrome); reversing twice is identity.

Background lessons

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.