# how to swap 2 numbers without using temporary variable

Page 1 of 1

## 8 Replies - 8967 Views - Last Post: 11 October 2009 - 04:35 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=131125&amp;s=7f94f677cc9886b8145c228014cdf326&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 rahul_ait

Reputation: 0
• Posts: 1
• Joined: 10-October 09

# how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 07:30 AM

how to swap 2 numbers without using temporary variable
Is This A Good Question/Topic? 0

## Replies To: how to swap 2 numbers without using temporary variable

### #2 Dogstopper

Reputation: 2898
• Posts: 11,122
• Joined: 15-July 08

## Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 07:47 AM

rahul_ait, on 10 Oct, 2009 - 06:30 AM, said:

how to swap 2 numbers without using temporary variable

Can this even be done? I mean, even pre-prepared Java swappers use a temporary variables... Why is this so important?

### #3 LaFayette

• D.I.C Regular

Reputation: 43
• Posts: 326
• Joined: 24-November 08

## Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 07:48 AM

Can be done with simple arithmetics!
```a = a + b;
b = a - b;
a = a - b;

```

Kinda stupid/unecessary to not use a temp variable though...

edited for stupidity!

This post has been edited by LaFayette: 10 October 2009 - 07:56 AM

### #4 AntonWebsters

• D.I.C Regular

Reputation: 88
• Posts: 428
• Joined: 15-August 09

## Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 08:19 AM

Please don't tell me that your lecturer asked you to write a method that swaps two values without using a temporary variable...@.@
I don't think it's possible.....right?

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11034
• Posts: 41,290
• Joined: 27-December 08

## Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 08:38 AM

This sounds like a homework assignment?

@OP: What do you think? Give us some of your ideas. We can help you more once you show us some effort. Thanks!

Reputation:

## Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 09:32 AM

### #7 skaoth

Reputation: 91
• Posts: 601
• Joined: 07-November 07

## Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 09:36 AM

This isn't the only way. There is also the xor swap

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

Edit: Looking again. It looks a lot like LaFayette's answer

This post has been edited by skaoth: 10 October 2009 - 09:37 AM

### #8 LaFayette

• D.I.C Regular

Reputation: 43
• Posts: 326
• Joined: 24-November 08

## Re: how to swap 2 numbers without using temporary variable

Posted 10 October 2009 - 10:45 AM

Quote

Thanks!

Quote

There is also the xor swap

I had almost forgotten about that one. It's actual pretty likely (I think...) that the compiler (javac in this case) can recognise simple swapping and translates it into somethings similar..

This post has been edited by LaFayette: 10 October 2009 - 10:45 AM

### #9 pbl

• There is nothing you can't do with a JTable

Reputation: 8362
• Posts: 31,955
• Joined: 06-March 08

## Re: how to swap 2 numbers without using temporary variable

Posted 11 October 2009 - 04:35 PM

Will also work with an XOR

A XOR B
B XOR A
A XOR B

fastest way to swap two register in assembler