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#
// C# Program to Swap the two Numbers // using Bitwise XOR Operation 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()); // printing the numbers before swapping Console.WriteLine("Numbers before swapping"); Console.WriteLine("x = {0} \t b = {1}", x, y); // swapping x = x ^ y; y = x ^ y; x = x ^ y; // printing the numbers after swapping Console.WriteLine("Numbers after swapping"); Console.WriteLine("x = {0} \t b = {1}", x, y); Console.ReadLine(); } } } |
chevron_right
filter_none
Output:
Enter two numbers Numbers before swapping x = 300 b = 400 Numbers after swapping x = 400 b = 300
Recommended Posts:
- C# | Swap two Strings without using third user defined variable
- C# Program for Nested Conditional Operator
- C# | Operator Overloading
- LINQ | Filtering Operator | OfType
- C# | as Operator Keyword
- C# | is Operator Keyword
- Is vs As operator keyword in C#
- typeof Operator Keyword in C#
- Null-Coalescing Assignment Operator in C# 8.0
- LINQ | Set Operator | Except
- LINQ | Element Operator | Last
- LINQ | Projection Operator | Select
- LINQ | Projection Operator | SelectMany
- LINQ | Filtering Operator | where
- LINQ | Sorting Operator | OrderBy
- LINQ | Sorting Operator | OrderByDescending
- LINQ | Sorting Operator | Reverse
- LINQ | Sorting Operator | ThenBy
- LINQ | Equality Operator | SequenceEqual
- LINQ | Set Operator | Distinct
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

