Skip to content

Commit 4d50fd6

Browse files
feat:Add SwapNumbersUsingXor algo with JUnit tests
1 parent ac65af4 commit 4d50fd6

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* [SingleBitOperations](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleBitOperations.java)
4646
* [SingleElement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SingleElement.java)
4747
* [SwapAdjacentBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapAdjacentBits.java)
48+
* [SwapNumbersUsingXor](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXor.java)
4849
* [TwosComplement](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/TwosComplement.java)
4950
* ciphers
5051
* a5
@@ -676,6 +677,7 @@
676677
* [SingleBitOperationsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleBitOperationsTest.java)
677678
* [SingleElementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SingleElementTest.java)
678679
* [SwapAdjacentBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapAdjacentBitsTest.java)
680+
* [SwapNumbersUsingXorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/SwapNumbersUsingXorTest.java)
679681
* [TwosComplementTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/TwosComplementTest.java)
680682
* ciphers
681683
* a5
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* <pre>
5+
* This class provides a method to swap two numbers using XOR without creating a third variable.
6+
*
7+
* The XOR swap algorithm works as follows:
8+
*
9+
* Let's say we have two numbers, a and b.
10+
*
11+
* Step 1: a = a ^ b
12+
* - This step stores the XOR of a and b in a.
13+
* - Example: If a = 5 (0101 in binary) and b = 3 (0011 in binary),
14+
* then a = 5 ^ 3 = 6 (0110 in binary).
15+
*
16+
* Step 2: b = a ^ b
17+
* - This step updates b to the original value of a.
18+
* - Example: Now a = 6 (0110 in binary) and b = 3 (0011 in binary),
19+
* then b = 6 ^ 3 = 5 (0101 in binary).
20+
*
21+
* Step 3: a = a ^ b
22+
* - This step updates a to the original value of b.
23+
* - Example: Now a = 6 (0110 in binary) and b = 5 (0101 in binary),
24+
* then a = 6 ^ 5 = 3 (0011 in binary).
25+
*
26+
* After these three steps, the values of a and b are swapped.
27+
*
28+
* For more information, refer to the
29+
* <a href="https://en.wikipedia.org/wiki/XOR_swap_algorithm"> XOR swap algorithm </a>.
30+
*
31+
* Example usage:
32+
* <code>
33+
* int[] result = SwapNumbersUsingXor.swap(5, 3);<br>
34+
* System.out.println("After swap: a = " + result[0] + ", b = " + result[1]);
35+
* </code>
36+
* </pre>
37+
*/
38+
public class SwapNumbersUsingXor {
39+
40+
/**
41+
* Swaps two numbers using XOR.
42+
*
43+
* @param a the first number
44+
* @param b the second number
45+
* @return an array containing the swapped numbers
46+
*/
47+
public static int[] swap(int a, int b) {
48+
a = a ^ b; // Step 1
49+
b = a ^ b; // Step 2
50+
a = a ^ b; // Step 3
51+
return new int[] {a, b};
52+
}
53+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the SwapNumbersUsingXor class.
9+
*/
10+
public class SwapNumbersUsingXorTest {
11+
12+
/**
13+
* Test the swap method with positive numbers.
14+
*/
15+
@Test
16+
public void testSwapPositiveNumbers() {
17+
int[] result = SwapNumbersUsingXor.swap(5, 3);
18+
assertArrayEquals(new int[] {3, 5}, result);
19+
}
20+
21+
/**
22+
* Test the swap method with negative numbers.
23+
*/
24+
@Test
25+
public void testSwapNegativeNumbers() {
26+
int[] result = SwapNumbersUsingXor.swap(-5, -3);
27+
assertArrayEquals(new int[] {-3, -5}, result);
28+
}
29+
30+
/**
31+
* Test the swap method with a positive and a negative number.
32+
*/
33+
@Test
34+
public void testSwapPositiveAndNegativeNumbers() {
35+
int[] result = SwapNumbersUsingXor.swap(5, -3);
36+
assertArrayEquals(new int[] {-3, 5}, result);
37+
}
38+
39+
/**
40+
* Test the swap method with zero.
41+
*/
42+
@Test
43+
public void testSwapWithZero() {
44+
int[] result = SwapNumbersUsingXor.swap(0, 3);
45+
assertArrayEquals(new int[] {3, 0}, result);
46+
}
47+
}

0 commit comments

Comments
 (0)