1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Swap two values using XOR operation

Discussion in 'C' started by madhu, Aug 30, 2005.

  1. madhu

    madhu New Member

    Joined:
    Jul 25, 2005
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Hi all, How we can Swap the two values using XOR operation but without using third Variable?
    Thanks
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Very simple
    Code:
    int x = 1;
    int y = 10;
    x = x^y;
    y = x^y;
    x = x^y;
    This is similar to doing with +- or */

    Thanks
    Shabbir Bhimani
     
  3. madhu

    madhu New Member

    Joined:
    Jul 25, 2005
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Thanks. I was not able to start my PC and so I could not reply.
     
  4. vignesh1988i

    vignesh1988i Banned

    Joined:
    Sep 19, 2009
    Messages:
    72
    Likes Received:
    0
    Trophy Points:
    0
    Location:
    Chennai
    we can do in a single line using compound assignment tooo....:)..
    x^=y^=x^=y;

    the above too will work


    thank u
     

Share This Page