LeetCode 371 Sum of Two Integers - Solution

The core idea

Break addition into two parts: sum without carry and carry.

1.1. XOR gives the sum where no carry occurs: a ^ b. 2.2. AND gives positions where both bits are 1 (carry needed), shifted left: (a & b) << 1.

Repeat until there's no carry. The XOR result is the final sum.

This simulates how a hardware adder works: half-adders compute sum and carry, then propagate.