Simvik

Swap byte order

Reverse the four bytes of a 32-bit value. An easy embedded C problem (Bit manipulation), run and graded in the browser.

easy · Bit manipulation · endianness

Return x with its bytes in the opposite order, so 0x12345678 becomes 0x78563412.

This converts between little-endian and big-endian representations of the same number.

Starter code

uint32_t bswap32(uint32_t x) {
  // Byte 0 <-> byte 3, byte 1 <-> byte 2.
  return x;
}

Example tests

Four distinct bytes

printf("0x%08X\n", bswap32(0x12345678));

Expected output:

0x78563412

Lowest byte moves to the top

printf("0x%08X\n", bswap32(0x000000FF));

Expected output:

0xFF000000

Learn it first

  • Bit manipulation · Masks, shifts, fields and the idioms every register access is built from.

More Bit manipulation problems