2026/04/13

[LeetCode] 371 Sum of Two Integers

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
public:
  int getSum(int a, int b) {
    while (b != 0) {
      int sum = a ^ b;
      int carry = (a & b) << 1;
      a = sum;
      b = carry;
    }
    return a;
  }
};