Created
March 27, 2018 18:06
-
-
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.
This file contains 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
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