All submissions

Carry Flag in 8-bit Addition

Code

#include <stdio.h>
#include <stdint.h>

void add_with_carry(uint8_t a, uint8_t b, uint8_t* result, uint8_t* carry) {
    uint8_t sum = 0;
    uint8_t carry_out = 0;
    for(int i = 0; i < 8; i++){
        sum |= ((a & 0x1) ^ (b & 0x1) ^ carry_out) << i;
        carry_out = ((a & 0x1) & (b & 0x1)) | (((a & 0x1) ^ (b & 0x1)) & carry_out);
        a >>= 1;
        b >>= 1;
    }
    *result = sum;
    *carry = carry_out;
}

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;
}

Solving Approach

 

 

 

Loading...

Input

100 50

Expected Output

150 0