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) {
    // Your logic here
    // *carry = 0;
    // for(uint8_t i=0;i<=b;i++){
    // *result = a+i;
    // if(*result>=255)
    // *carry = 1;
    // }

    uint16_t temp = a+b;
    *result = (uint8_t)temp;
    *carry = (temp > 255);

}

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

 

Step :- 1 uint16_t temp variable initialize

    uint16_t temp = a+b;

Step :- 2 typecasting the temp by uint8_t and storing in *result 

    *result = (uint8_t)temp;

Step :- 3 checking the temp greater than  255 then storing carry

    *carry = (temp > 255);

Loading...

Input

100 50

Expected Output

150 0