+ 1
Add two numbers without using operator + ????
2 odpowiedzi
+ 3
x=5;
y=3;
z=x-(-y);
// z=8
+ 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;
}
}