Simvik

Set a bit

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

easy · Bit manipulation · masks

Return reg with bit pos set to 1. Every other bit must keep its value.

Bit 0 is the least significant bit, and pos is always between 0 and 7.

Starter code

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

Example tests

Set bit 3 of an empty register

printf("0x%02X\n", set_bit(0x00, 3));

Expected output:

0x08

Other bits are preserved

printf("0x%02X\n", set_bit(0x81, 4));

Expected output:

0x91

Learn it first

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

More Bit manipulation problems