Thursday, January 2, 2014

Addition without using arithmetic(+) operator

public class Addition {

       public int sum(int a, int b) {
              while (b != 0) {
                     int carry = a & b;
                     a = a ^ b;
                     b = carry << 1;
              }
              return a;
       }
      
       public static void main(String[] args) {
              Addition a = new Addition();
              int sum = a.sum(7, 10);
              System.out.println("Sum : " + sum);
       }
}


No comments:

Post a Comment