Simvik

Add with carry

Add two bytes and report whether the result carried out of 8 bits. An easy embedded C problem (Integers and data types), run and graded in the browser.

easy · Integers and data types · overflow · uint8_t

An 8-bit CPU sets a carry flag when an addition does not fit in a byte. Reproduce that in C.

Store the low 8 bits of a + b in *sum and return true if the addition carried out of 8 bits.

Starter code

bool add_u8(uint8_t a, uint8_t b, uint8_t *sum) {
  // Store the low 8 bits of a + b in *sum.
  // Return true if the true result needed a ninth bit.
  return false;
}

Example tests

No carry

uint8_t s = 0;
bool c = add_u8(100, 50, &s);
printf("%u %d\n", s, c);

Expected output:

150 0

Carry out

uint8_t s = 0;
bool c = add_u8(200, 100, &s);
printf("%u %d\n", s, c);

Expected output:

44 1

Learn it first

More Integers and data types problems