#include <stdio.h>
#include <stdint.h>
#define signbit 0x80
void add_with_carry(uint8_t a, uint8_t b, uint8_t* result, uint8_t* carry) {
// Your logic here
uint8_t i, reg=0, c=0, bita=0, bitb=0 ;
for (i=0; i < 8; i++) {
bita = a & 1;
bitb = b & 1;
reg = reg >> 1;
if ((bita == 1) && (bitb == 1)) {
if (c == 0) {
c = 1;
reg = (reg | 0);
}
else {
c = 1;
reg = (reg | 0x80);
}
}
if ((bita == 1) && (bitb == 0)) {
if (c == 0) {
c = 0;
reg = (reg | 0x80);
}
else {
c = 1;
reg = (reg | 0);
}
}
if ((bita == 0) && (bitb == 0)) {
if (c == 0) {
c = 0;
reg = (reg | 0);
}
else {
c = 0;
reg = (reg | 0x80);
}
}
if ((bita == 0) && (bitb == 1)) {
if (c == 0) {
c = 0;
reg = (reg | 0x80);
}
else {
c = 1;
reg = (reg | 0);
}
}
a = a >> 1;
b = b >> 1;
}
*result = reg;
*carry = c;
}
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;
}
add the regisers bit by bit with carry.
isolate the bits by shifting right and masking higer bits.
put results in register from the end and shift it right
Input
100 50
Expected Output
150 0