How to add two numbers using bitwise operators in C

1 Answer

0 votes
#include <stdio.h>

int bitwise_add(int x, int y) {
    while (y != 0) {
        int carry = x & y;
        x = x ^ y; 
        y = carry << 1;
    }

    return x;
}

int main(void) {
    printf("%d", bitwise_add(3, 7));
     
    return 0;
}
  
     
     
     
/*
run :
 
10
 
*/

 



answered Feb 15, 2021 by avibootz

Related questions

3 answers 370 views
2 answers 282 views
2 answers 251 views
1 answer 167 views
1 answer 214 views
...