Regarding Bit to Bit Operators in a sum operation

Asked

Viewed 196 times

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/

  • a andb are input variables and c is an intermediate variable. All are used in the loop to compute the sum being accumulated in b. 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 of a and b 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.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.