Simvik

Find the highest ready priority

Pick the highest priority from a 32-bit ready bitmap. An easy embedded C problem (RTOS scheduling), run and graded in the browser.

easy · RTOS scheduling · rtos · scheduler · bits

Many RTOS kernels keep one bit per priority level: bit p is set when some task at priority p is ready. Higher numbers are more important.

Return the highest priority with its bit set, or -1 if no bit is set.

Starter code

int8_t highest_ready(uint32_t ready) {
  for (int8_t p = 0; p < 32; p++)
    if (ready & (1u << p)) return p;
  return -1;
}

Example tests

Two ready priorities

printf("%d\n", highest_ready(0x00000012u));

Expected output:

4

The top and bottom bits

printf("%d\n", highest_ready(0x80000001u));

Expected output:

31

Learn it first

More RTOS scheduling problems