bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 371. Sum of Two Integers

371. Sum of Two Integers

Leetcode Solutions

Bit Manipulation for Sum without + or - Operators

  1. Initialize carry.
  2. While carry is not zero: a. Calculate answer as the XOR of a and b to add without carrying. b. Calculate carry as the AND of a and b, then left shift by 1 to determine the carry. c. Update a to be answer (the partial sum without carry). d. Update b to be carry (the carry that needs to be added).
  3. Return a as the final sum.
UML Thumbnail

Iterative Addition using Bit Unset Positions and Carry

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...