How many different ways can you come up with to swap the values of two variables without using any third variable ?
I use this one:
a^=b^=a^=b
How many different ways can you come up with to swap the values of two variables without using any third variable ?
I use this one:
a^=b^=a^=b
Read this in my first book on C (Programming with C -- Sudhir Kaicker):
a += b;
b = a - b;
a = a - b;
a = a + b
b = a - b
a = a - b
There are many ways to do this. Trivially replacing +- with */ will also do it.
Generalising your example slightly, whenever two binary operations #
and @
are inverses of each other, the following sequence will exchange the values of a
and b
:
a = a # b
b = a @ b
a = a @ b
Your example works because ^
(XOR) is its own inverse.
This is a neat trick. Unfortunately, some people think it makes a good interview question.
[EDIT: Thanks to mouviciel for correcting an error on the 2nd line.]
temp = a ; a = b ; b = temp ;
'Clever' ways are just noise, IMO.
Write code for people first, computers 2nd.
PHP
list($a, $b) = array($b, $a);
JavaScript 1.7
[a, b] = [b, a]
Python
a, b = b, a
I asked this question awhile back:
How does XOR variable swapping work?
It got some really good responses if you are curious how this is possible.
My Forth is way rusty, but:
. A
. B
@ A
@ B
(Does "@" write the top of the stack to a variable, or am I rustier than I thought? Anyway, this should work for all stack-based languages.)
If you're using the standard Forth idiom of treating stack entries as temporary variables, there's always:
SWAP
EDIT: I didn't mean to offend the sensibility of those of you who use dynamic languages. They are very useful tools, but, to me, the definition of a variable is something that is in the call stack and isn't the address in memory of the instruction the program must return to after the current subroutine finishes. And those nice library functions and thingies you're used to have tons of temporary results. Those temporary results must be stored somewhere in the memory, whether you like to call it a variable or not.
EDIT: The optimal implementation of the XOR swap doesn't involve any temporary results that don't have to be stored in temporary variables (not even in machine code!). This is what I meant. Using the XCHG instruction is a nice alternative.
Intel Assembly
Assuming the variables a, b are defined in .data:
mov eax, a
xchg b, eax
mov a, eax
In Lua:
a,b = b,a
You have to like it. It goes for any number of vars.
You didn't mention in what language tho :P
In assembly language of 8051 family , suppose X= 50 Y= 30
mov r1,#50 // r1=50 i.e. x
mov r0,#30 // r0=30 i.e. y
mov r2,r1 // r2=50, r1=50, r0=30
mov r1,r0 // r1=30, r0=30, r2=50
mov r0,r2 // r0=50, r1=30, r2=50
Hence initially X=r1=50 and Y=r0=30
after the sequence X=r1=30 and Y=r0=50
Note :- r0, r1 and r2 are registers of the 8051 microcontroller
Works in many languages
Works only with numbers
a = a + b - (b = a);
This is a simple method, logic can be implemented all the languages... Happy Sensible Coding..
$v = 'sriram';
$u = 'lakshmi';
$v .= $u;
$u = substr($v,0,(strlen($v) - strlen($u)));
$v = substr($v,(strlen($v) - strlen($u)-1), strlen($v));
echo 'u = ' . $u .'<br>';
echo 'v = ' . $v;
#include <iostream>
using namespace std();
void main()
{
int a=59;
int b=67;
a=a+b;
b=a-b;
a=a-b;
cout<<"a="<<a<<"b="<<b<<endl;
}