+ 1

Add two numbers without using operator + ????

10th Dec 2016, 12:35 PM
ravi
2 Respuestas
+ 3
x=5; y=3; z=x-(-y); // z=8
10th Dec 2016, 12:55 PM
Andrew Ward
Andrew Ward - avatar
+ 2
The following is an iterative solution using bitwise instead of arithmetic operators. You can find a more thorough explanation here:( http://javarevisited.blogspot.com/2013/06/how-to-add-two-integer-numbers-without-plus-arithmetic-operator-java-example.html?m=1) Hope this is helpful, goodluck. class MyClass { public static void main(String[ ] args) { System.out.println(add(1,2)); } public static int add(int a, int b) { while (b != 0) { int c = (a & b) ; a = a ^ b; b = c << 1; } return a; } }
10th Dec 2016, 1:24 PM
Gary Chisenhall
Gary Chisenhall - avatar