basics · intermediate · ~15 min

Byte-swap a 32-bit value

Move bytes between positions with shifts and masks.

Challenge

Implement uint32_t swap_u32(uint32_t x) reversing the byte order (0x11223344 -> 0x44332211).

Starter code

#include <stdint.h>

uint32_t swap_u32(uint32_t x) {
    /* TODO */
    return 0;
}

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