fork download
  1. /**
  2.  * Add two numbers without '+' operator refer:
  3.  * http://e...content-available-to-author-only...a.org/wiki/Adder_%28electronics%29#Half_adder
  4.  * @author PRATEEK
  5.  */
  6. class AddNumbers {
  7.  
  8. public static int add1(int a, int b) {
  9. while (b != 0) {
  10. int carry = a & b;
  11. a = a ^ b;
  12. b = carry << 1;
  13. }
  14.  
  15. return a;
  16. }
  17.  
  18. public static int add2(int a, int b) {
  19. if (b == 0)
  20. return a;
  21. else
  22. return add2(a ^ b, (a & b) << 1);
  23. }
  24.  
  25. public static int add3(int a,int b){
  26. int carry= a & b;
  27. int sum= a ^ b;
  28.  
  29. carry = 2* carry;
  30.  
  31. sum = sum ^ carry;
  32.  
  33. return sum;
  34.  
  35. }
  36.  
  37. public static int add4(int a,int b){
  38. return ( 2*( a & b) ) ^ (a ^ b);
  39.  
  40. }
  41.  
  42. public static void main(String[] args) {
  43. int val1=add1(5, 6);
  44. int val2=add2(5, 6);
  45. int val3=add3(5, 6);
  46. int val4=add4(5, 6);
  47.  
  48. System.out.println(val1 + " "+val2 + " "+val3 + " "+val4 + " ");
  49.  
  50. }
  51. }
  52.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
11  11  11  11