Skip to main content
Back to problems
#371
Medium Algorithms

Sum of two integers

Math Bit Manipulation
55.1% acceptance
Jan 12, 2026
4654
5862
Given two integers a and b, return the sum of the two integers without using the operators + and -.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn get_sum(a: i32, b: i32) -> i32 {
    let mut a = a;
    let mut b = b;
    
    while b != 0 {
      let carry = (a & b) << 1;
      a = a ^ b;
      b = carry;
    }
    
    a
  }
}