Simvik

Clear a bit

Turn off one bit of a register value. An easy embedded C problem (Bit manipulation), run and graded in the browser.

easy · Bit manipulation · masks

Return reg with bit pos cleared to 0. Every other bit must keep its value. pos is between 0 and 7.

Starter code

uint8_t clear_bit(uint8_t reg, uint8_t pos) {
  // Return reg with bit `pos` turned off.
  return reg;
}

Example tests

Clear bit 0

printf("0x%02X\n", clear_bit(0xFF, 0));

Expected output:

0xFE

Other bits are preserved

printf("0x%02X\n", clear_bit(0x18, 3));

Expected output:

0x10

Learn it first

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

More Bit manipulation problems