From 4d50fd6bde3ad1762045ea61afe0c9ae903b5183 Mon Sep 17 00:00:00 2001 From: Tanmay Singh Date: Sat, 12 Oct 2024 16:14:51 +0530 Subject: [PATCH 1/3] feat:Add SwapNumbersUsingXor algo with JUnit tests --- DIRECTORY.md | 2 + .../bitmanipulation/SwapNumbersUsingXor.java | 53 +++++++++++++++++++ .../SwapNumbersUsingXorTest.java | 47 ++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXorTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index 30fa2cbee199..1acb5758b679 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -45,6 +45,7 @@ * [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java) * [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java) * [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java) + * [SwapNumbersUsingXor](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java) * [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java) * ciphers * a5 @@ -676,6 +677,7 @@ * [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java) * [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java) * [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java) + * [SwapNumbersUsingXorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXorTest.java) * [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java) * ciphers * a5 diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java b/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java new file mode 100644 index 000000000000..91eddabbd7b6 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java @@ -0,0 +1,53 @@ +package com.thealgorithms.bitmanipulation; + +/** + *
+ * This class provides a method to swap two numbers using XOR without creating a third variable.
+ *
+ * The XOR swap algorithm works as follows:
+ *
+ * Let's say we have two numbers, a and b.
+ *
+ * Step 1: a = a ^ b
+ * - This step stores the XOR of a and b in a.
+ * - Example: If a = 5 (0101 in binary) and b = 3 (0011 in binary),
+ *   then a = 5 ^ 3 = 6 (0110 in binary).
+ *
+ * Step 2: b = a ^ b
+ * - This step updates b to the original value of a.
+ * - Example: Now a = 6 (0110 in binary) and b = 3 (0011 in binary),
+ *   then b = 6 ^ 3 = 5 (0101 in binary).
+ *
+ * Step 3: a = a ^ b
+ * - This step updates a to the original value of b.
+ * - Example: Now a = 6 (0110 in binary) and b = 5 (0101 in binary),
+ *   then a = 6 ^ 5 = 3 (0011 in binary).
+ *
+ * After these three steps, the values of a and b are swapped.
+ *
+ * For more information, refer to the
+ *  XOR swap algorithm .
+ *
+ * Example usage:
+ * 
+ * int[] result = SwapNumbersUsingXor.swap(5, 3);
+ * System.out.println("After swap: a = " + result[0] + ", b = " + result[1]); + *
+ *
+ */ +public class SwapNumbersUsingXor { + + /** + * Swaps two numbers using XOR. + * + * @param a the first number + * @param b the second number + * @return an array containing the swapped numbers + */ + public static int[] swap(int a, int b) { + a = a ^ b; // Step 1 + b = a ^ b; // Step 2 + a = a ^ b; // Step 3 + return new int[] {a, b}; + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXorTest.java b/src/test/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXorTest.java new file mode 100644 index 000000000000..01b2f1f5a48a --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXorTest.java @@ -0,0 +1,47 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; + +import org.junit.jupiter.api.Test; + +/** + * Unit tests for the SwapNumbersUsingXor class. + */ +public class SwapNumbersUsingXorTest { + + /** + * Test the swap method with positive numbers. + */ + @Test + public void testSwapPositiveNumbers() { + int[] result = SwapNumbersUsingXor.swap(5, 3); + assertArrayEquals(new int[] {3, 5}, result); + } + + /** + * Test the swap method with negative numbers. + */ + @Test + public void testSwapNegativeNumbers() { + int[] result = SwapNumbersUsingXor.swap(-5, -3); + assertArrayEquals(new int[] {-3, -5}, result); + } + + /** + * Test the swap method with a positive and a negative number. + */ + @Test + public void testSwapPositiveAndNegativeNumbers() { + int[] result = SwapNumbersUsingXor.swap(5, -3); + assertArrayEquals(new int[] {-3, 5}, result); + } + + /** + * Test the swap method with zero. + */ + @Test + public void testSwapWithZero() { + int[] result = SwapNumbersUsingXor.swap(0, 3); + assertArrayEquals(new int[] {3, 0}, result); + } +} From f2453fce642841b3430082e7c257a07db07446f2 Mon Sep 17 00:00:00 2001 From: Tanmay Singh Date: Sat, 12 Oct 2024 16:38:01 +0530 Subject: [PATCH 2/3] fix: Hide the constructor for SwapNumbersUsingXor to make it an utility class --- .../thealgorithms/bitmanipulation/SwapNumbersUsingXor.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java b/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java index 91eddabbd7b6..e65dd7a32111 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java @@ -26,9 +26,9 @@ * After these three steps, the values of a and b are swapped. * * For more information, refer to the - * XOR swap algorithm . + * XOR swap algorithm. * - * Example usage: + * Example usage: * * int[] result = SwapNumbersUsingXor.swap(5, 3);
* System.out.println("After swap: a = " + result[0] + ", b = " + result[1]); @@ -36,7 +36,8 @@ * */ public class SwapNumbersUsingXor { - + private SwapNumbersUsingXor() { + } /** * Swaps two numbers using XOR. * From 028ca746d4966609b9de010da65de6c4d1a18ff2 Mon Sep 17 00:00:00 2001 From: Tanmay Singh Date: Sat, 12 Oct 2024 16:50:46 +0530 Subject: [PATCH 3/3] fix: Make the SwapNumbersUsingXor class a final class --- .../com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java b/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java index e65dd7a32111..f94c05316388 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java @@ -35,7 +35,7 @@ *
* */ -public class SwapNumbersUsingXor { +public final class SwapNumbersUsingXor { private SwapNumbersUsingXor() { } /**