Repository: TheAlgorithms/Java
Pull request #3473 (2022-10-09 20:26:11)

Code for #3435 - Sum numbers without using any mathematical operators
### **Describe your change:**
Added an algorithm to sum two numbers without using any mathematical operators
- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

This PR is in reference to #3435 
<!-- Add any reference to a previous pull request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in their comments that points to Wikipedia or similar explanations.


CenTdemeern1: Suggestion for an alternative name:
Addition through (recursive) bit manipulation

Patch:
diff --git a/src/main/java/com/thealgorithms/maths/SumWithoutArithmeticOperators.java b/src/main/java/com/thealgorithms/maths/SumWithoutArithmeticOperators.java
new file mode 100644
index 00000000..f45ac4d3
--- /dev/null
+++ b/src/main/java/com/thealgorithms/maths/SumWithoutArithmeticOperators.java
@@ -0,0 +1,20 @@
+package com.thealgorithms.maths;
+
+public class SumWithoutArithmeticOperators {
+
+    /**
+        * Calculate the sum of two numbers a and b without using any arithmetic operators (+, -, *, /).
+        * All the integers associated are unsigned 32-bit integers
+        *https://stackoverflow.com/questions/365522/what-is-the-best-way-to-add-two-numbers-without-using-the-operator
+        *@param a - It is the first number 
+        *@param b - It is the second number
+        *@return returns an integer which is the sum of the first and second number
+    */ 
+
+    public int getSum(int a, int b){
+        if(b==0) return a;
+        int sum = a^b;
+        int carry = (a&b)<<1;
+        return getSum(sum, carry);
+    }
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java b/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java
new file mode 100644
index 00000000..ad2158ac
--- /dev/null
+++ b/src/test/java/com/thealgorithms/maths/SumWithoutArithmeticOperatorsTest.java
@@ -0,0 +1,39 @@
+package com.thealgorithms.maths;
+
+import static org.junit.jupiter.api.Assertions.*;
+import org.junit.jupiter.api.Test;
+
+public class SumWithoutArithmeticOperatorsTest {
+    SumWithoutArithmeticOperators obj = new SumWithoutArithmeticOperators();
+
+    @Test
+    void addZerotoZero(){
+        assertEquals(0,obj.getSum(0, 0));
+    }
+
+    @Test
+    void addZerotoNumber(){
+        assertEquals(5,obj.getSum(0, 5));
+        assertEquals(28,obj.getSum(28, 0));
+    }
+
+    @Test
+    void addOddtoEven(){
+        assertEquals(13,obj.getSum(3, 10));
+        assertEquals(55,obj.getSum(49, 6));
+    }
+
+    @Test
+    void addEventoOdd(){
+        assertEquals(13,obj.getSum(10, 3));
+        assertEquals(41,obj.getSum(40, 1));
+    }
+
+    @Test
+    void addRandoms(){
+        assertEquals(88,obj.getSum(44, 44));
+        assertEquals(370,obj.getSum(100, 270));
+        assertEquals(3,obj.getSum(1, 2));
+        assertEquals(5,obj.getSum(2, 3));
+    }
+}