0
Hello I have this part of program and I would like to know why should be used these assignments being that only prints the variable b
int soma()
{
int a,b,c;
printf("Digite o numero que deseja somar\n");
scanf("%d", &a);
printf("Digite o segundo numero que deseja somar\n");
scanf("%d", &b);
while (a != 0)
{
c = b & a;
b = b ^ a;
c = c << 1;
a = c;
}
printf("Soma: %d\n",b);
}
Here is a somewhat cleaner version of this algorithm that separates the sum into a dedicated function: http://www.sanfoundry.com/c-program-perform-addition-operation-bitwise-operators/
– Anthony Accioly
a
andb
are input variables andc
is an intermediate variable. All are used in the loop to compute the sum being accumulated inb
. This is an algorithm for simulating sums through bit manipulation. I don’t really like reassigning input variables during computing; I prefer to copy the values ofa
andb
for auxiliary variables before the while and have an extra variable (e.g., sum) for the result. Unfortunately this type of code comes back and forth in exercises.– Anthony Accioly