|
|||||||
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
#1 |
|
Member (7 bit)
|
Java swap algorithm
Hey guys, I have a quick question. I missed the day when we learned the swap algorithm, and I can't find it online. How do I swap the values of two integer variables without using a third temporary variable?
|
|
|
|
|
|
#2 |
|
Come in Ray...
Join Date: Sep 2004
Posts: 1,668
|
If you can figure that out, you can make a lot of money. I don't believe it is possible.
|
|
|
|
|
|
#3 |
|
Registered User
Join Date: Nov 2001
Posts: 1,965
|
Actually it is possible, here is one way:
Code:
x ^= y; y ^= x; x ^= y; http://en.wikipedia.org/wiki/Xor_swap_algorithm |
|
|
|
|
|
#4 |
|
Come in Ray...
Join Date: Sep 2004
Posts: 1,668
|
Wow, never considered bitwise manipulation... interesting link aym, it actually says using the temporary variable is faster.
Thanks for pointing it out! |
|
|
|
|
|
#5 |
|
Member (7 bit)
|
thanks for the helpful response! the wikipedia article was also informative.
![]() damn, I love pcmech! |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|