Simvik

Bitmap operations

Set, clear and test individual bits in a byte array. An easy embedded C problem (Buffers and data structures), run and graded in the browser.

easy · Buffers and data structures · bitmap

A bitmap stores one flag per bit in an array of bytes. Bit number bit lives in byte bit / 8, at position bit % 8 within that byte.

Implement the three basic operations.

Starter code

void bitmap_set(uint8_t *map, size_t bit) {
}

void bitmap_clear(uint8_t *map, size_t bit) {
}

bool bitmap_test(const uint8_t *map, size_t bit) {
  return false;
}

Example tests

Set bits in two bytes

uint8_t map[2] = { 0, 0 };
bitmap_set(map, 0);
bitmap_set(map, 9);
printf("%02X %02X\n", map[0], map[1]);

Expected output:

01 02

Test bits

uint8_t map[2] = { 0x01, 0x02 };
printf("%d %d %d\n", bitmap_test(map, 9), bitmap_test(map, 8), bitmap_test(map, 0));

Expected output:

1 0 1

Learn it first

More Buffers and data structures problems