#include <stdio.h> #include <stdint.h> void add_with_carry(uint8_t a, uint8_t b, uint8_t* result, uint8_t* carry) { *result = a + b; // Count how many bits not set uint8_t a_to_overflow; // Solution 1 // uint8_t shift = a; // for(int i = 0; i < 8; i++) { // if(!(shift & 1U)) { // a_to_overflow |= 1 << i; // } // shift >>= 1; // } // Solution 2 a_to_overflow = a ^ 0xFF; if (b > a_to_overflow) { *carry = 1; } } int main() { uint8_t a, b, result, carry; scanf("%hhu %hhu", &a, &b); add_with_carry(a, b, &result, &carry); printf("%u ", result); printf("%u", carry); return 0; }
Test Cases
Test Results
Input
100 50
Expected Output
150 0