Skip to content

Instantly share code, notes, and snippets.

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 sanjeevshrestha/e479db756802a5e302fa87f90dfd725b to your computer and use it in GitHub Desktop.
Save sanjeevshrestha/e479db756802a5e302fa87f90dfd725b to your computer and use it in GitHub Desktop.
Adding numbers without + Operator in Java
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