Wednesday, 28 October 2015

C# Program to Swap the Contents of two Numbers using Bitwise XOR Operation?

class SwapWithXOR
{
    public static void Main()
    {
        int j, k;
        Console.WriteLine("Enter two integers \n");
        j= int.Parse(Console.ReadLine());
        k = int.Parse(Console.ReadLine());
        Console.WriteLine("\n Before swapping j= {0} and k = {1}", j, k);
        j= j^ k;
        k = j^ k;
        j= j^ k;
        Console.WriteLine("\n After swapping j= {0} and k = {1}", j, k);
        Console.ReadLine();
    }
}
------------------------
OUTPUT
Enter two integers
30
40
Before swapping j= 30 and k = 40
After swapping j= 40 and k = 30
------------------------

1 comment:

  1. thanks for the good update, please continue it.

    ReplyDelete