Simvik

Signed against unsigned

Compare a signed value with an unsigned limit correctly. An easy embedded C problem (Integers and data types), run and graded in the browser.

easy · Integers and data types · signed-unsigned · comparison

Return true if value is mathematically less than limit.

A negative value is below every possible limit. Writing value < limit gets that case wrong.

Starter code

bool is_below(int32_t value, uint32_t limit) {
  // value < limit converts value to unsigned first, so -1 becomes 4294967295.
  return value < limit;
}

Example tests

Positive value

printf("%d\n", is_below(5, 10));

Expected output:

1

Negative value

printf("%d\n", is_below(-1, 10));

Expected output:

1

Learn it first

More Integers and data types problems