/**
* Add two numbers without '+' operator refer:
* http://e...content-available-to-author-only...a.org/wiki/Adder_%28electronics%29#Half_adder
* @author PRATEEK
*/
class AddNumbers {
public static int add1(int a, int b) {
while (b != 0) {
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
public static int add2(int a, int b) {
if (b == 0)
return a;
else
return add2(a ^ b, (a & b) << 1);
}
public static int add3(int a,int b){
int carry= a & b;
int sum= a ^ b;
carry = 2* carry;
sum = sum ^ carry;
return sum;
}
public static int add4(int a,int b){
return ( 2*( a & b) ) ^ (a ^ b);
}
public static void main
(String[] args
) { int val1=add1(5, 6);
int val2=add2(5, 6);
int val3=add3(5, 6);
int val4=add4(5, 6);
System.
out.
println(val1
+ " "+val2
+ " "+val3
+ " "+val4
+ " ");
}
}
LyoqCiAqIEFkZCB0d28gbnVtYmVycyB3aXRob3V0ICcrJyBvcGVyYXRvciByZWZlcjoKICogaHR0cDovL2UuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLmEub3JnL3dpa2kvQWRkZXJfJTI4ZWxlY3Ryb25pY3MlMjkjSGFsZl9hZGRlcgogKiBAYXV0aG9yIFBSQVRFRUsKICovCmNsYXNzIEFkZE51bWJlcnMgewoJCglwdWJsaWMgc3RhdGljIGludCBhZGQxKGludCBhLCBpbnQgYikgewoJCXdoaWxlIChiICE9IDApIHsKCQkJaW50IGNhcnJ5ID0gYSAmIGI7CgkJCWEgPSBhIF4gYjsKCQkJYiA9IGNhcnJ5IDw8IDE7CgkJfQoJCQoJCXJldHVybiBhOwoJfQoKCXB1YmxpYyBzdGF0aWMgaW50IGFkZDIoaW50IGEsIGludCBiKSB7CgkJaWYgKGIgPT0gMCkKCQkJcmV0dXJuIGE7CgkJZWxzZQoJCQlyZXR1cm4gYWRkMihhIF4gYiwgKGEgJiBiKSA8PCAxKTsKCX0KCglwdWJsaWMgc3RhdGljIGludCBhZGQzKGludCBhLGludCBiKXsKCQlpbnQgY2Fycnk9IGEgJiBiOwo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