#include <stdio.h> #include <stdint.h> void subtract_with_underflow(uint8_t a, uint8_t b, uint8_t *diff, uint8_t *carry) { uint16_t temp = (uint16_t)a - (uint16_t)b; *diff = (uint8_t)(temp & 0xFF); // get lower 8 bits *carry = (a < b) ? 1 : 0; // underflow (borrow) detection } int main() { uint8_t a, b, diff, carry; scanf("%hhu %hhu", &a, &b); subtract_with_underflow(a, b, &diff, &carry); printf("diff = %u, carry = %u\n", diff, carry); return 0; }
Test Cases
Test Results
Input
100 50
Expected Output
diff = 50, carry = 0