Created
July 6, 2016 19:16
-
-
Save sanjeevshrestha/e479db756802a5e302fa87f90dfd725b to your computer and use it in GitHub Desktop.
Adding numbers without + Operator in Java
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
public static int addIter(int a, int b) { | |
while (b != 0) { | |
int carry = a & b; | |
a = a ^ b; | |
b = carry << 1; | |
} | |
return a; | |
} | |
public static int addRec(int a, int b) { | |
if (b == 0) { | |
return a; | |
} | |
return addRec(a ^ b, (a & b) << 1); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment