Go Back   PCMech Forums > Help & Discussion > Web Design / Development

Need Some Help? Type Your Keywords Here:

Reply
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
Old 03-24-2005, 12:38 PM   #1
Member (7 bit)
 
raja's Avatar
 
Join Date: Oct 2001
Location: Macon, Georgia
Posts: 93
Send a message via ICQ to raja Send a message via AIM to raja
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?
raja is offline   Reply With Quote
Old 03-24-2005, 12:59 PM   #2
Come in Ray...
 
faulkner132's Avatar
 
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.
faulkner132 is offline   Reply With Quote
Old 03-26-2005, 04:02 AM   #3
aym
Registered User
 
aym's Avatar
 
Join Date: Nov 2001
Posts: 1,965
Actually it is possible, here is one way:

Code:
x ^= y;
y ^= x;
x ^= y;
More info:
http://en.wikipedia.org/wiki/Xor_swap_algorithm
aym is offline   Reply With Quote
Old 03-27-2005, 01:22 AM   #4
Come in Ray...
 
faulkner132's Avatar
 
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!
faulkner132 is offline   Reply With Quote
Old 04-08-2005, 06:43 AM   #5
Member (7 bit)
 
raja's Avatar
 
Join Date: Oct 2001
Location: Macon, Georgia
Posts: 93
Send a message via ICQ to raja Send a message via AIM to raja
thanks for the helpful response! the wikipedia article was also informative.

damn, I love pcmech!
raja is offline   Reply With Quote
Reply

Bookmarks

Still Need Help? Type Your Keywords Here:


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 07:19 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
SEO by vBSEO 3.6.0 PL2