Simvik

Test a bit

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

easy · Bit manipulation · masks

Return 1 if bit pos of value is set and 0 if it is clear. pos is between 0 and 31.

The return type is uint8_t, so make sure the result really is 0 or 1.

Starter code

uint8_t bit_is_set(uint32_t value, uint8_t pos) {
  // Return 1 if bit `pos` is set, otherwise 0.
  return 0;
}

Example tests

Bit is set

printf("%u\n", bit_is_set(0x10, 4));

Expected output:

1

Bit is clear

printf("%u\n", bit_is_set(0x10, 3));

Expected output:

0

Learn it first

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

More Bit manipulation problems