Created
November 4, 2015 13:41
-
-
Save carlosgaldino/40bcbfd2af44b7bc3ace to your computer and use it in GitHub Desktop.
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> | |
/* Bitwise integer addition */ | |
int plus(int x, int y) | |
{ | |
int result = x ^ y; | |
int carry = (x & y) << 1; | |
if (carry != 0) | |
return plus(result, carry); | |
return result; | |
} | |
int main(int argc, char **argv) | |
{ | |
printf("%d\n", plus(3, 4)); | |
printf("%d\n", plus(5, 6)); | |
printf("%d\n", plus(5, 10)); | |
printf("%d\n", plus(35, 100)); | |
printf("%d\n", plus(-8, 2)); | |
printf("%d\n", plus(93, -208)); | |
printf("%d\n", plus(-93, -208)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment