Sum of Two Integers
MediumUpdated: Sep 19, 2025
Practice on:
Problem
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Follow up
Write a function Add(a, b) that returns the sum of two integers without using any arithmetic operators (+, -, *, /, ++, --, etc.). Use only bitwise operators and shifts where needed.
Examples
Example 1
Input: a = 1, b = 2
Output: 3
Solution
See Method 1 and Method 2 of [Sum of two numbers using only bitwise operators](sum-of-two-numbers-using-only-bitwise-operators), and here is the video explaining this method in detail. Please check it out:
<div class="youtube-embed"><iframe src="https://www.youtube.com/embed/NJLGft5fS2Y" frameborder="0" allowfullscreen></iframe></div>