Swap Two Variables in One Line
Last Updated :
21 Jun, 2022
We have discussed different approaches to swap two integers without the temporary variable. How to swap into a single line without using the library function?
1) Python: In Python, there is a simple and syntactically neat construct to swap variables, we just need to write "x, y = y, x".
2) C/C++: Below is one generally provided classical solution:
// Swap using bitwise XOR (Wrong Solution in C/C++)
x ^= y ^= x ^= y;
The above solution is wrong in C/C++ as it causes undefined behavior (the compiler is free to behave in any way). The reason is, modifying a variable more than once in an expression causes undefined behavior if there is no sequence point between the modifications.
However, we can use a comma to introduce sequence points. So the modified solution is
// Swap using bitwise XOR (Correct Solution in C/C++)
// sequence point introduced using comma.
(x ^= y), (y ^= x), (x ^= y);
3) Java: In Java, rules for subexpression evaluations are clearly defined. The left-hand operand is always evaluated before the right-hand operand. In Java, the expression "x ^= y ^= x ^= y;" doesn't produce the correct result according to Java rules. It makes x = 0. However, we can use "x = x ^ y ^ (y = x);" Note the expressions are evaluated from left to right. If x = 5 and y = 10 initially, the expression is equivalent to "x = 5 ^ 10 ^ (y = 5);". Note that we can't use this in C/C++ as in C/C++, it is not defined whether the left operand or right operand is executed by any operator (See this for more details).
4) JavaScript: Using destructing assignment, we can simply achieve swapping using this one line.
[x,y]=[y,x]
C
// C program to swap two variables in single line
#include <stdio.h>
int main()
{
int x = 5, y = 10;
(x ^= y), (y ^= x), (x ^= y);
printf("After Swapping values of x and y are %d %d", x,
y);
return 0;
}
C++
// C++ code to swap using XOR
#include <bits/stdc++.h>
using namespace std;
int main()
{
int x = 5, y = 10;
// Code to swap 'x' and 'y'
// to swap two numbers in one
// line
x = x ^ y, y = x ^ y, x = x ^ y;
// printing the swapped variables
cout << "After Swapping: x = "
<< x << ", y= " << y;
return 0;
}
Java
// Java program to swap two variables in a single line
class GFG {
public static void main(String[] args)
{
int x = 5, y = 10;
x = x ^ y ^ (y = x);
System.out.println(
"After Swapping values"
+" of x and y are " + x
+ " " + y);
}
}
Python3
# Python program to swap two variables in a single line
x = 5
y = 10
x, y = y, x
print("After Swapping values of x and y are", x, y)
C#
// C# program to swap two
// variables in single line
using System;
class GFG {
static public void Main()
{
int x = 5, y = 10;
x = x ^ y ^ (y = x);
Console.WriteLine("After Swapping values "
+ "of x and y are " + x + " "
+ y);
}
}
// This code is contributed by aj_36
PHP
<?php
// PHP program to swap two
// variables in single line
// Driver Code
$x = 5;
$y = 10;
($x ^= $y);
($y ^= $x);
($x ^= $y);
echo "After Swapping values of x and y are "
,$x," ", $y;
// This code is contributed by Vishal Tripathi
?>
JavaScript
<script>
// javascript program to swap two variables in single line
let x = 5, y = 10;
(x ^= y), (y ^= x), (x ^= y);
document.write("After Swapping values of x and y are ", x + " ",
y);
// This code is contributed by Surbhi Tyagi
</script>
Output
After Swapping values of x and y are 10 5
Alternate Solutions:
- Using swap(): C++ library function
- b = (a + b) - (a = b);
- a += b - (b = a);
- a = a * b / (b = a)
- a = a ^ b ^ (b = a)
Time complexity : O(1)
Auxiliary Space : O(1)
Similar Reads
Swap Two Numbers Without Using Third Variable Given two variables a and y, swap two variables without using a third variable. Examples: Input: a = 2, b = 3Output: a = 3, b = 2Input: a = 20, b = 0Output: a = 0, b = 20Input: a = 10, b = 10Output: a = 10, b = 10Table of ContentUsing Arithmetic OperatorsUsing Bitwise XORBuilt-in SwapUsing Arithmeti
6 min read
Swap three numbers in cycle Given three numbers, swap them in cyclic form. First number should get the value of third, second should get the value of first and third should get value of second. Examples: Input : a = 2, b = 4, c = 7 Output : a = 7, b = 2, c = 4 Input : a = 10, b = 20, c = 30 Output : a = 30, b = 10, c = 20 Prer
5 min read
Move spaces to front of string in single traversal Given a string that has set of words and spaces, write a program to move all spaces to front of string, by traversing the string only once. Examples: Input : str = "geeks for geeks" Output : str = " geeksforgeeks" Input : str = "move these spaces to beginning" Output : str = " movethesespacestobegin
6 min read
Swap Two Numbers Given two numbers a and b, the task is to swap them.Examples: Input: a = 2, b = 3Output: a = 3, b = 2Input: a = 20, b = 0Output: a = 0, b = 20Input: a = 10, b = 10Output: a = 10, b = 10 Table of Content[Naive Approach] Using Third Variable[Expected Approach] Without using Third Variable[Alternate Ap
4 min read
Javascript program to swap two numbers without using temporary variable To swap two numbers without using a temporary variable, we have multiple approaches. In this article, we are going to learn how to swap two numbers without using a temporary variable. Below are the approaches used to swap two numbers without using a temporary variable: Table of Content Using Arithme
6 min read
Swapping four variables without temporary variable Suppose we have four variables a, b, c, d and we want to perform swapping of these variables in the following manner a = b, b = c, c = d, d = a without using any other fifth or temporary variable Solution : Step 1. Swap a and b without using any other variable a = a + b b = a - b a = a - b Step 2. S
7 min read