Important alert: (current site time 6/18/2013 7:01:00 PM EDT)
 

VB icon

Swap Magic

Email
Submitted on: 6/11/2002 4:34:50 PM
By: FAzle AreFin  
Level: Beginner
User Rating: By 6 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 2384
(About the author)
 
     Do you think it is possible to swap values between two variables without using a third temporary variable? What? No? Yes? Well, the answer is...YES. Take a look at the code and don't forget to vote!
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Swap Magic
// Description:Do you think it is possible to swap values between two variables without using a third temporary variable? What? No? Yes? Well, the answer is...YES. Take a look at the code and don't forget to vote!
// By: FAzle AreFin
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=2973&lngWId=2//for details.//**************************************

/*	
	Swap Magic
	Swaps values between two variables without using a third temporary variable
	Programmer: Fazle Arefin
*/
	
public class SwapMagic {
	public static void main(String args[]) {
		// declare and initialize two variables
		int a = 10;
 		int b = 20;
 		
 		System.out.println("Before swapping a = " + a + " and b = " + b);
 		a = a + b;
 		b = a - b;
 		a= a - b;
 		
 		/*	
 			instead of + and - you can use * and / respectively
 			a = a * b;
 			b = a / b;
 			a= a / b;
 		
 			instead of + and - or * and / you can use the XOR operator ^
 			but this restricts you to only swapping integers
 			a = a ^ b;
 			b = a ^ b;
 			a= a ^ b;
 		*/
 		
 		System.out.println("Before swapping a = " + a + " and b = " + b);
 }
}
 	


Other 6 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/12/2002 12:56:40 AMMartin Budden

It's almost useless but I love it!
Very neat.
(If this comment was disrespectful, please report it.)

 
6/12/2002 6:04:54 AMMarco Cioffi

I love the xor version!!
(If this comment was disrespectful, please report it.)

 
6/12/2002 10:53:03 PMRAMGarden

Sweet jezus! I would NEVER had thought to do it that way! I soooo should have thought of this. lol Good job!
(If this comment was disrespectful, please report it.)

 
7/17/2002 10:50:44 AMAlastair

Ah good ideas never die they just get recoded, seem to remember using the XOR version back in assembler days.
(If this comment was disrespectful, please report it.)

 
8/26/2002 4:58:44 AMDreamlax

Nicely done, but it won't work for strings... never mind I needed it for numbers anyway!
(If this comment was disrespectful, please report it.)

 
3/13/2004 11:00:03 AM

wow, its not used very often but when u use it, its amazing...i never got the xor version to work
(If this comment was disrespectful, please report it.)

 
6/28/2004 11:03:39 PMYu Jiunn Shyang

may i have your w-mail address? my e-mail address is adrianyu_j_s@yahoo.com. I need help in Java programming.
(If this comment was disrespectful, please report it.)

 
11/20/2005 11:38:06 AMgren

Ha, perfect example of "out of the box".

Kudos on your discovery.
(yes i know it's been done before)
(If this comment was disrespectful, please report it.)

 
9/30/2006 2:48:57 PMRakesh

The one with + and - is well known.
Didn't know that it would even work with */ or with XOR.
Nice
(If this comment was disrespectful, please report it.)

 
12/31/2006 1:17:59 PMBasel Nimer

* and / will not work if either of the values is 0,

I can't believe that someone is still in need for this Dreamlax!!! :D

(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.