Simvik

Build a bit mask

Create a mask with a contiguous range of bits set. A medium embedded C problem (Bit manipulation), run and graded in the browser.

medium · Bit manipulation · masks · undefined-behaviour

Return a 32-bit mask with bits hi down to lo set (both inclusive) and every other bit clear. lo <= hi <= 31.

For example make_mask(7, 4) is 0x000000F0. The full range make_mask(31, 0) must work too.

Starter code

uint32_t make_mask(uint8_t hi, uint8_t lo) {
  // Bits hi..lo set, everything else clear.
  return 0;
}

Example tests

Upper nibble of a byte

printf("0x%08X\n", make_mask(7, 4));

Expected output:

0x000000F0

Lowest nibble

printf("0x%08X\n", make_mask(3, 0));

Expected output:

0x0000000F

Learn it first

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

More Bit manipulation problems