Skip to content

Instantly share code, notes, and snippets.

@exhesham
Created March 27, 2018 18:06
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 exhesham/5b10389e76024af8d8ff18f97c07e9c3 to your computer and use it in GitHub Desktop.
Save exhesham/5b10389e76024af8d8ff18f97c07e9c3 to your computer and use it in GitHub Desktop.
Write a function that adds two numbers. You should not use + or any arithmetic operators.
int add_no_arithm(int a, int b) {
if (b == 0) return a;
int sum = a ^ b; // add without carrying
int carry = (a & b) << 1; // carry, but don’t add
return add_no_arithm(sum, carry); // recurse
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment