المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : Swaping without Temp Var



phiras
05-03-2005, 06:42 AM
Al Salam Alikom
I have 2 integers x&y .
I Want to Swap thos values .
this Code is works very good :

temp = x;
x = y;
y = temp ;

Now you have to do this Operation without Using the temp var ,
:09:

ibn_alqalam
05-03-2005, 04:19 PM
GAWD, are you _itching_ for the old XOR trick? can I cheat and use implicit temporaries in function arguments?




int x, y;

void swap (int a, int b)
{
x = b;
y = a;
}

[initialize x and y here]

swap (x, y);

phiras
07-03-2005, 12:38 AM
GAWD, are you _itching_ for the old XOR trick? can I cheat and use implicit temporaries in function arguments?




int x, y;

void swap (int a, int b)
{
x = b;
y = a;
}

[initialize x and y here]

swap (x, y);


are you Keding man ? :D.
1- Calling the function swap() is creating 2 Vars in the Memory, so it dosn't work.
2- you cn do it with XOR func , but there is another Easy way .
Just think:09: