Skip to content

Commit c233833

Browse files
feature:Add SwapNumbersUsingXor algo with JUnit test
1 parent ac65af4 commit c233833

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
* <b>Example usage:</b>
29+
* <code>
30+
* int[] result = SwapNumbersUsingXor.swap(5, 3);<br>
31+
* System.out.println("After swap: a = " + result[0] + ", b = " + result[1]);
32+
* </code>
33+
* </pre>
34+
*/
35+
public class SwapNumbersUsingXor {
36+
37+
/**
38+
* Swaps two numbers using XOR.
39+
*
40+
* @param a the first number
41+
* @param b the second number
42+
* @return an array containing the swapped numbers
43+
*/
44+
public static int[] swap(int a, int b) {
45+
a = a ^ b; // Step 1
46+
b = a ^ b; // Step 2
47+
a = a ^ b; // Step 3
48+
return new int[]{a, b};
49+
}
50+
}
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)