Last active
April 2, 2019 16:01
-
-
Save Yoxem/c984b51c488ea4749a3abbde03f6e8a6 to your computer and use it in GitHub Desktop.
addition of 2 unsigned int with bitwise operator in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
// add with bitwise operators | |
unsigned int add_binary(unsigned int x, unsigned int y){ | |
unsigned int a = x ^ y; | |
unsigned int b = (x & y); | |
if (b == 0){ | |
return a; | |
} | |
else{ | |
b = b << 1; | |
return add_binary(a, b); | |
} | |
} | |
int main(){ | |
unsigned int a = 12; | |
unsigned int b = 78; | |
unsigned int c = add_binary(12, 78); // 90 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment