Simvik

Toggle a bit

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

easy · Bit manipulation · masks · xor

Return reg with bit pos inverted. pos is between 0 and 31.

Starter code

uint32_t toggle_bit(uint32_t reg, uint8_t pos) {
  // Return reg with bit `pos` flipped.
  return reg;
}

Example tests

Flip the top bit on

printf("0x%08X\n", toggle_bit(0x00000000, 31));

Expected output:

0x80000000

Flip the bottom bit off

printf("0x%08X\n", toggle_bit(0x0000000F, 0));

Expected output:

0x0000000E

Learn it first

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

More Bit manipulation problems