Simvik

Will the multiplication overflow?

Decide whether a 32-bit unsigned product fits in 32 bits. A medium embedded C problem (Integers and data types), run and graded in the browser.

medium · Integers and data types · overflow · multiplication

Return true if a * b does not fit in a uint32_t.

This is the check calloc has to make before it multiplies the element count by the element size.

Starter code

bool mul_overflows(uint32_t a, uint32_t b) {
  // Return true when the exact product a * b exceeds UINT32_MAX.
  return false;
}

Example tests

Just over

printf("%d\n", mul_overflows(65536, 65536));

Expected output:

1

Exactly the maximum

printf("%d\n", mul_overflows(65535, 65537));

Expected output:

0

Learn it first

More Integers and data types problems