Simvik

Fix: the ready check fires too early

A check that two status flags are both set passes when only one of them is. An easy embedded C problem (Bit manipulation), run and graded in the browser.

easy · Bit manipulation · find-the-bug · precedence · masks

Bug report. sample_ready(status) must return true only when both READY (bit 0) and VALID (bit 3) are set in a status register value, whatever the other bits are. On the bench it returns true as soon as READY is set, so the code reads half-converted samples. Fix it by changing one line.

Starter code

bool sample_ready(uint32_t status) {
  return status & ST_BOTH == ST_BOTH;
}

Example tests

Both, only READY, only VALID

printf("%d %d %d\n", sample_ready(0x09), sample_ready(0x01), sample_ready(0x08));

Expected output:

1 0 0

Learn it first

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

More Bit manipulation problems