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..60035f25127b --- /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..6c3e1d21ea20 --- /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); + } +}