basics · intermediate · ~15 min
Reverse the bit order of a 32-bit word.
Implement:
uint32_t reverse_bits(uint32_t x);
Return x with its 32 bits reversed (bit 0 <-> 31, 1 <-> 30, ...).
A 32-bit value.
The bit-reversed value.
None.
#include <stdint.h>
/* Reverse the 32 bits of x (bit 0<->31, 1<->30, ...). */
uint32_t reverse_bits(uint32_t x){ return x; }
Shifting a uint8_t/int accumulator that can't hold 32 bits.
Reversing twice yields the original (an involution).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.