Skip to content

Instantly share code, notes, and snippets.

@carlosgaldino
Created November 4, 2015 13:41
Show Gist options
  • Save carlosgaldino/40bcbfd2af44b7bc3ace to your computer and use it in GitHub Desktop.
Save carlosgaldino/40bcbfd2af44b7bc3ace to your computer and use it in GitHub Desktop.
#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