Return to Snippet

Revision: 43231
at March 19, 2011 18:29 by kanuwadhwa


Initial Code
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
	<mx:Script>
		<![CDATA[
			public function init():void{
				var a:Number = 5;
				var b:Number = 6;
		
				// solution 2 : using xor
				a = a^b; // a = 1^2 = 1
				b = a^b; // b = 1^2 = 1
				a = a^b; // a = 1^1 = 1
				txt.text ="\n XOR Method >>> a: "+a+" b: "+b; 
				
				// solution 1
				a=a+b; // a = 1+2 = 3
				b=a-b; // b = a-b = 3-2 = 1
				a=a-b; // a = 3-1 = 2
				txt.text += "\n Solution 1 >>> a: "+a+" b: "+b;
				
				// solution 3
				a=a*b;
				b=a/b;
				a=a/b;
				txt.text +="\n Solution 3 >>> a: "+a+" b: "+b; 				
			} 
		]]>
	</mx:Script>
	<mx:TextArea x="118" y="57" width="553" height="412" id="txt"/>
</mx:Application>

Initial URL

                                

Initial Description
Looking forward for the most optimized solution for the same

Initial Title
Swapping of two numbers without using a third variable

Initial Tags
actionscript, flash, Flex

Initial Language
ActionScript 3