Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example:
Given a = 1 and b = 2, return 3.
Tags:Bit Manipulation
Similar Problems: (M) Add Two Numbers
1 class Solution { 2 public: 3 int getSum(int a, int b) 4 { 5 while (b) 6 { 7 int sum = a ^ b; 8 int carray = (a & b) << 1; 9 a = sum; 10 b = carray; 11 } 12 return a; 13 } 14 };
原文:http://www.cnblogs.com/whl2012/p/5690159.html