https://leetcode.com/problems/sum-of-two-integers/
Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -.
Example 1:
Input: a = 1, b = 2 Output: 3 Example 2:
Input: a = -2, b = 3 Output: 1
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MathQuestions
{
/// <summary>
/// https://leetcode.com/problems/sum-of-two-integers/
/// </summary>
[TestClass]
public class SumOfTwoIntegersTest
{
[TestMethod]
public void ExampleTest()
{
int a = 1;
int b = 2;
int expected = 3;
Assert.AreEqual(expected, GetSum(a,b));
}
[TestMethod]
public void ExampleTest2()
{
int a = 5;
int b = 7;
int expected = 12;
Assert.AreEqual(expected, GetSum(a, b));
}
public int GetSum(int a, int b)
{
if (a == 0) return b;
if (b == 0) return a;
while (b != 0)
{
int carry = a & b;
a = a ^ b;
b = carry << 1;
}
return a;
}
}
}
Please review for performance