Simvik

Swap nibbles

Exchange the high and low halves of a byte. An easy embedded C problem (Bit manipulation), run and graded in the browser.

easy · Bit manipulation · nibbles · bcd

Return x with its two 4-bit halves exchanged, so 0xA5 becomes 0x5A.

Starter code

uint8_t swap_nibbles(uint8_t x) {
  // Exchange bits 7..4 with bits 3..0.
  return x;
}

Example tests

Different nibbles

printf("0x%02X\n", swap_nibbles(0xA5));

Expected output:

0x5A

One empty nibble

printf("0x%02X\n", swap_nibbles(0xF0));

Expected output:

0x0F

Learn it first

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

More Bit manipulation problems