18. swap using xor

 X = 5  = 0101

Y = 9  = 1001


x = x ^ y

   = 0101

   = 1001

   = 1100 ( = 12 )

y = x ^ y

   = 1100

   = 1001

   = 0101 ( = 5 = Y)

 x = x ^ y

    = 1100

    = 0101

    = 1001 ( = 9 = X)




Code :

#include<bits/stdc++.h>

using namespace std;

int main()

{   

    int x,y,t;

    cin>>t;

    while (t--)

    {

        cin>>x>>y;

        x = x^y;

        y = x^y;

        x = x^y;

        cout<<x<<' '<<y<<endl;

    }

}








Comments