-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add addition without arithmetic algorithm
- Loading branch information
Showing
3 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
Algorithms.Tests/Numeric/AdditionWithoutArithmeticsTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System; | ||
using System.Numerics; | ||
using Algorithms.Numeric; | ||
using NUnit.Framework; | ||
|
||
namespace Algorithms.Tests.Numeric; | ||
|
||
public static class AdditionWithoutArithmeticTests | ||
{ | ||
[TestCase(3, 5, 8)] | ||
[TestCase(13, 5, 18)] | ||
[TestCase(-7, 2, -5)] | ||
[TestCase(0, -7, -7)] | ||
[TestCase(-321, 0, -321)] | ||
public static void CalculateAdditionWithoutArithmetic_Test(int first, int second, int expectedResult) | ||
{ | ||
// Act | ||
var result = AdditionWithoutArithmetic.CalculateAdditionWithoutArithmetic(first, second); | ||
|
||
// Assert | ||
Assert.That(result, Is.EqualTo(expectedResult)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using System; | ||
using System.Numerics; | ||
|
||
namespace Algorithms.Numeric; | ||
|
||
/// <summary> | ||
/// Add the integers without arithmetic operation. | ||
/// </summary> | ||
public static class AdditionWithoutArithmetic | ||
{ | ||
/// <summary> | ||
/// Returns the sum of two integers. | ||
/// </summary> | ||
/// <param name="first">First number to add.</param> | ||
/// <param name="second">Second number to add.</param> | ||
/// <returns>Sum of the two numbers.</returns> | ||
public static int CalculateAdditionWithoutArithmetic(int first, int second) | ||
{ | ||
while (second != 0) | ||
{ | ||
int c = first & second; // Carry | ||
first ^= second; // Sum without carry | ||
second = c << 1; // Carry shifted left | ||
} | ||
Check failure on line 24 in Algorithms/Numeric/AdditionWithoutArithmetic.cs GitHub Actions / build
Check failure on line 24 in Algorithms/Numeric/AdditionWithoutArithmetic.cs GitHub Actions / build
|
||
return first; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters