basics · intermediate · ~15 min
Move bytes between positions with shifts and masks.
Reverse the byte order of a 32-bit integer — the core of converting between endiannesses.
Implement uint32_t swap_u32(uint32_t x) that returns x with its four bytes in reverse order, so 0x11223344 becomes 0x44332211. Move each byte to its mirrored position using shifts and masks. No main — the grader calls it.
A single uint32_t x.
Returns x with its byte order reversed, as a uint32_t.
swap_u32(0x11223344) -> 0x44332211
swap_u32(0x000000FF) -> 0xFF000000
swap_u32(0) -> 0
<arpa/inet.h> or builtins required).A single uint32_t x.
x with its four bytes reversed, as a uint32_t.
Use shifts and masks.
#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.