# Swap two numbers without using a temporary variable



## PcEnthu (Apr 17, 2008)

Ok guys. Here is the problem. There are two variables x and y which hold numbers. You have to swap the value of the two variables. U might say, just store the value of x in a temporary variable z, store value of y in x and finally store z value in y   Here is the tricky part, u have to do it without using the variable z


----------



## mediator (Apr 17, 2008)

x=a
y=b
x=x+y
y=x-y
x=x-y

show x,y


----------



## Kalyan (Apr 17, 2008)

x,y be the nos.

1) x=x+y
2) y=x-y
3) x=x-y

u got them swapped.


----------



## VINSTAR (Apr 17, 2008)

x=x+y
y=x-y
x=x-y


----------



## ray|raven (Apr 17, 2008)

You can even use division and multiplication operators.

x=x*y
y=x/y
x=x/y


----------



## PcEnthu (Apr 17, 2008)

@ all u guys, That was fast and u got that cracked that easily. Well done. I will try to come up with some level up questions later.


----------



## QwertyManiac (Apr 17, 2008)

Or if you wish to do it the l33t way, use XOR.

Starting with x=10, y=20

x^=y
y^=x
x^=y

Now x=20 and y=10.
Logic's the same as addition mostly, except its bitwise. 

Edit: This a challenge or a homework question?  For challenges, there exists a thread, I hope you can revive it with _better_ questions than the ones already present.


----------



## blueshift (Apr 17, 2008)

// 'a' and 'b' are the two variables.

a=a^b
b=a^b
a=a^b


----------



## PcEnthu (Apr 17, 2008)

rayraven said:


> You can even use division and multiplication operators.
> 
> x=x*y
> y=x/y
> x=x/y



That would work for positive and negative numbers and not for zero


----------



## ray|raven (Apr 17, 2008)

^Yep. Thats one limitation, by using div.


----------



## Jove (Apr 21, 2008)

yeah...XORing is much better as compared to other suggested methods(here).
Other methods may not work if we are dealing with MAX possibles values of respective data types which results in overflow..so,
               if(a!=b) ///if a==b, no need of swapping values
                 a^=b^=a^=b;
         swaps the values in a and b

cheers


----------



## Desi-Tek.com (Apr 22, 2008)

x = 2
y = 4

y = y-x  = 2
x = y+y = 4


----------



## ray|raven (Apr 22, 2008)

Desi-Tek.com said:


> x = 2
> y = 4
> 
> x = y-x  = 2
> y = x+x = 4



Lolz, that works only for those no's i guess.

Try these,
x=5
y=7

x = y-x = 7-5 = 2 ?
y = x+x = 2+2 = 4 ?

Crappy logic.


----------



## hariharan (Apr 22, 2008)

i can do that in a single statement.. swap two number in w/o temp var


x ^= y ^= x ^= y;


This swaps the two varaibles... OF course in c and other languages which support bit wise operators..!!


----------



## QwertyManiac (Apr 22, 2008)

@rayraven - Lol!  By the way, D-Tek.com and Dheeraj are one and the same I think.



Desi-Tek.com said:


> x = 2
> y = 4
> 
> x = y-x  = 2
> y = x+x = 4


Hahah, moreover you started with x=2 and y=4 and ended with the same


----------



## ray|raven (Apr 22, 2008)

^Lolz, i missed that part 
What was he thinking while writing it?

I've known guys who try to deduce logic from examples.
Crappy way, if you ask me. 99% of the time you end up with a logic that works only for the example.


----------



## Desi-Tek.com (Apr 22, 2008)

oh sorry i am awake from 24 hour 

public
class Swap{
staticvoid swap(int x,int y){
//int temp=x;
x^=y; //2
y^=x; // 5
x^=y; //7
System.out.println("After swapping x = " + x + " y = " + y);
}
publicstaticvoid main(String[] args){
int x=5;
int y=7;

System.out.println("Before swapping x="+x+" y="+y);
swap(x,y);

}

}

//one line logic

public class Swap{
  static void swap(int x,int y){
      y=(x+y)-(x=y);
    System.out.println("After swapping x= " + x + " y = " + y);
  }
  public static void main(String[] args){
    int x=5;
    int y=7;

    System.out.println("Before swapping x="+x+" y="+y);
    swap(x,y);

  }
}


----------



## aditya.shevade (Apr 22, 2008)

LOL this has been discussed before....

anther way apart from the XOR way is,

b = a + b - (a = b); you are done.


----------



## eggman (Apr 22, 2008)

Desi-Tek.com said:


> x = 2
> y = 4
> 
> x = y-x  = 2
> y = x+x = 4



Ha ha ha ha ha ha ha ha ha ha ha ha ha ha 

I've never seen a post funnier and noobier than this!!!!!


----------



## mehulved (Apr 24, 2008)

eggman said:


> Ha ha ha ha ha ha ha ha ha ha ha ha ha ha
> 
> I've never seen a post funnier and noobier than this!!!!!


I have.


----------



## aditya.shevade (Apr 28, 2008)

Oi mehul.... he said he has not... he did not say you have not....


----------



## Zeeshan Quireshi (Apr 29, 2008)

In Ruby:

```
b,a = a,b
```


----------



## ray|raven (Apr 29, 2008)

aditya.shevade said:


> Oi mehul.... he said he has not... he did not say you have not....



Sarcasm perhaps?


----------



## aditya.shevade (Apr 29, 2008)

^^ yeah... I guess 

Was it Mehul?


----------



## Vivek788 (Apr 29, 2008)

well u can use these in a loop or condition statement like the if or for to avoid a semicolon.


----------

