hi, i tried with the following code , but i can't understand why it's giving me wrong answer. i am computing the 2's complement and adding with another no.
#include <stdio.h>
int add(int a, int b) {
while (a) {
a = (a & b) << 1;
b = a^b;
}
return b;
}
int sub(int a, int b) // add a with b's 2's complement.
{
return (add(a, add(~b, 1)));
}
int main() {
int a, b, res;
a = 3, b = 1;
res = sub(a, b);
printf("%d\n", res);
return 0;
}