Skip to content

Instantly share code, notes, and snippets.

@zhangzhiqiangcs
Created July 22, 2017 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhangzhiqiangcs/58c74da99b7656b38e471c4faa10a593 to your computer and use it in GitHub Desktop.
Save zhangzhiqiangcs/58c74da99b7656b38e471c4faa10a593 to your computer and use it in GitHub Desktop.
int getSum(int a, int b) {
int sum = a;
while (b != 0)
{
sum = a ^ b;//calculate sum of a and b without thinking the carry
b = (a & b) << 1;//calculate the carry
a = sum;//add sum(without carry) and carry
}
return sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment