Posts Sum of Two Integers (Bit Manipulation)
Post
Cancel

Sum of Two Integers (Bit Manipulation)

This question is part of NeetCode150 series.

Problem Description

Given two integers a and b, return the sum of the two integers without using the operators + and -.

leetcode

Solution

NeetCode YouTube

snapshot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
    
    public int getSum(int a, int b) {

        while(b != 0){

            int temp = a&b;

            a = a ^ b;
            b = temp<<1;

        }

        return a;


    }

}
This post is licensed under CC BY 4.0 by the author.