Can any one tell me how to add two numbers using bitwise operators only.

After spending so much time on googling,i found the below java code.But is
is confusing.
<[email protected]>public class AddTwoNumbers {

    private static int myAdd(int a, int b)
    {
        int carry = a & b;
        int result = a ^ b;
        while(carry != 0)
        {
            int shiftedcarry = carry << 1;
            carry = result & shiftedcarry;
            result ^= shiftedcarry;
        }
        return result;
    }

    public static void main(String[] args){
         System.out.println(myAdd(4, 5));
    }

}


Please explain it.

-- 
Thank You
Rajeev Kumar

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to