Program to swap numbers using XOR operator in C#
C# Program to swap the two numbers using Bitwise XOR Operation. Given two variables, x and y, swap two variables with using a XOR statements.
Example:
Input: 300 400
Output: 400 300
Explanation:
x = 300 y = 400
x = 400 y = 300
C#
using System;
using System.Text;
namespace Test {
class GFG {
static void Main( string [] args) {
int x, y;
Console.WriteLine( "Enter two numbers \n" );
x = int .Parse(Console.ReadLine());
y = int .Parse(Console.ReadLine());
Console.WriteLine( "Numbers before swapping" );
Console.WriteLine( "x = {0} \t b = {1}" , x, y);
x = x ^ y;
y = x ^ y;
x = x ^ y;
Console.WriteLine( "Numbers after swapping" );
Console.WriteLine( "x = {0} \t b = {1}" , x, y);
Console.ReadLine();
}
}
}
|
Output:
Enter two numbers
Numbers before swapping
x = 300 b = 400
Numbers after swapping
x = 400 b = 300
Last Updated :
08 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...