Skip to content

Instantly share code, notes, and snippets.

@Yoxem
Last active April 2, 2019 16:01
Show Gist options
  • Save Yoxem/c984b51c488ea4749a3abbde03f6e8a6 to your computer and use it in GitHub Desktop.
Save Yoxem/c984b51c488ea4749a3abbde03f6e8a6 to your computer and use it in GitHub Desktop.
addition of 2 unsigned int with bitwise operator in C
#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