Introduction
Swapping two numbers is a fundamental programming task, often used in various algorithms and data manipulation processes. Typically, a third variable is used to temporarily hold one of the values during the swap. However, in this post, we’ll explore how to swap two numbers without using a third variable in Java.
Why Swap Without a Third Variable?
Swapping without a third variable can be an interesting problem-solving exercise that sharpens your understanding of arithmetic operations and bitwise operators. It can also be useful in scenarios with limited memory resources.
Methods to Swap Two Numbers
- Using Arithmetic Operations
- Using Bitwise XOR Operator
Let’s delve into both methods with Java examples.
Method 1: Using Arithmetic Operations
This method utilizes addition and subtraction to swap values.
Code Example
public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("Before swap: a = " + a + ", b = " + b);
// Swapping without a third variable
a = a + b; // a becomes 30
b = a - b; // b becomes 10
a = a - b; // a becomes 20
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Explanation
- Addition:
a = a + b;
Adds both numbers and stores the result ina
. - Subtraction for
b
:b = a - b;
Subtractsb
from the newa
to get the original value ofa
. - Subtraction for
a
:a = a - b;
Subtracts the newb
(originala
) from the newa
to get the original value ofb
.
Method 2: Using Bitwise XOR Operator
This method uses the XOR bitwise operator for swapping.
Code Example
public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("Before swap: a = " + a + ", b = " + b);
// Swapping without a third variable using XOR
a = a ^ b; // a becomes 30 (in binary: 1010 ^ 10100)
b = a ^ b; // b becomes 10 (original value of a)
a = a ^ b; // a becomes 20 (original value of b)
System.out.println("After swap: a = " + a + ", b = " + b);
}
}
Explanation
- XOR Operation:
a = a ^ b;
Performs XOR operation and stores the result ina
. - XOR for
b
:b = a ^ b;
Performs XOR on the newa
andb
to get the original value ofa
. - XOR for
a
:a = a ^ b;
Performs XOR on the newa
and the newb
to get the original value ofb
.
Advantages of Each Method
- Arithmetic Method: Simple and easy to understand.
- Bitwise Method: More efficient for low-level programming and systems with limited arithmetic operation support.
Conclusion
Swapping two numbers without a third variable can be achieved efficiently using arithmetic operations or bitwise operators. This technique is a great exercise in understanding basic operations and can be handy in specific programming contexts.
Feel free to experiment with both methods and see which one fits your needs best. For more programming tutorials and code examples, stay tuned to our blog!
Additional Resources
Complete Code Snippet
Here is the complete Java code combining both methods:
public class SwapNumbers {
public static void main(String[] args) {
// Using Arithmetic Operations
int a1 = 10, b1 = 20;
System.out.println("Before swap (Arithmetic): a = " + a1 + ", b = " + b1);
a1 = a1 + b1; // a1 becomes 30
b1 = a1 - b1; // b1 becomes 10
a1 = a1 - b1; // a1 becomes 20
System.out.println("After swap (Arithmetic): a = " + a1 + ", b = " + b1);
// Using Bitwise XOR Operator
int a2 = 30, b2 = 40;
System.out.println("Before swap (Bitwise): a = " + a2 + ", b = " + b2);
a2 = a2 ^ b2; // a2 becomes 14 (in binary: 11110 ^ 101000)
b2 = a2 ^ b2; // b2 becomes 30 (original value of a2)
a2 = a2 ^ b2; // a2 becomes 40 (original value of b2)
System.out.println("After swap (Bitwise): a = " + a2 + ", b = " + b2);
}
}