Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addition without arithmetic algorithm #482

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions Algorithms.Tests/Numeric/AdditionWithoutArithmeticsTests.cs
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));
}
}
27 changes: 27 additions & 0 deletions Algorithms/Numeric/AdditionWithoutArithmetic.cs
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

View workflow job for this annotation

GitHub Actions / build

Check failure on line 24 in Algorithms/Numeric/AdditionWithoutArithmetic.cs

View workflow job for this annotation

GitHub Actions / build

return first;
}
}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ find more than one implementation for the same objective but using different alg
* [Modular Multiplicative Inverse](./Algorithms/ModularArithmetic/ModularMultiplicativeInverse.cs)
* [Numeric](./Algorithms/Numeric)
* [Absolute](./Algorithms/Numeric/Abs.cs)
* [Addition Without Arithmetic](./Algorithms/Numeric/AdditionWithoutArithmetic.cs)
* [Aliquot Sum Calculator](./Algorithms/Numeric/AliquotSumCalculator.cs)
* [Amicable Numbers Checker](./Algorithms/Numeric/AmicableNumbersChecker.cs)
* [Decomposition](./Algorithms/Numeric/Decomposition)
Expand Down
Loading