![]() |
Swap two variables using macro
The code to swap two variables using macro expansions
Code: C
|
Swap two numbers without using temp variable
//Code for swapping two numbers without using temp variable
Code: CPP
|
Re: Swap two variables using macro
better method is
usage of #define swap(a,b) a^=b^=a^=b; |
Re: Swap two variables using macro
better method is
usage of #define swap(a,b) a^=b^=a^=b; one line swap without using temporary variable. |
Re: Swap two variables using macro
Hello nicenaidu, i have compiler the file you post in MS VS 2005 but the IDE complaint that cstiod.h error .
Thanks for your help. Your help is greatly appreciated by me and others. |
Re: Swap two variables using macro
Here is a suggestion...
Using ^ operator is not the best way of swapping two values. Just consider is soemone try to SWAP same variable, like SWAP(A,A) what will happen? A^A will clear the all of the bits of A, because of which A will lost its original value on calling SWAP macro. Further swapping value using equation like A+B is not advisable , if A and B are very large values than result of this addition may cross the maximum value which an integer can store. We may end up with a incorrect result. So we should not try to play smart with fancy instruction as far as swapping is concerned. |
Re: Swap two variables using macro
Lets speak of C/C++
Why use a Macro and fix a type? Template functions can do the work cleaner. Code: CPP
|
Re: Swap two variables using macro
This can be done in Macro also…
Try this… #define SWAP (A, B) struct tempStruct { char C[sizeof(A)];} swap_tmp;\ swap_tmp = *( struct tempStruct*) &A;\ *( struct tempStruct*) &A = *( struct tempStruct*) &B;\ *( struct tempStruct*) &B = swap_tmp; |
Re: Swap two variables using macro
Correct to :p
Code:
#define SWAP(A, B) {struct tempStruct { char C[sizeof(A)];} swap_tmp;\Code: CPP
|
Re: Swap two variables using macro
>better method is
>usage of >#define swap(a,b) a^=b^=a^=b; > >one line swap without using temporary variable what exactly does the assignment operator ^= do? |
Re: Swap two variables using macro
a^=b is equivalent to a = a ^ b
|
Re: Swap two variables using macro
I answered my own question...
^= is a binary XOR assignment; It seems to run really slow however... I wrote 2 versions of the same sorting function, 1 using a temp variable and the other using this method. When i ran it on my P4 sort_xor took 24 seconds while sort_temp took 15. Code:
#include <stdio.h> |
Re: Swap two variables using macro
Yes you are correct. and following is the reason...
analyze following assembly code temp = x mov eax,dword ptr [x] mov dword ptr [temp],eax x = y; mov eax,dword ptr [y] mov dword ptr [x],eax y = temp; mov eax,dword ptr [temp] mov dword ptr [y],eax 6 mov instruction.... x ^= y ^= x ^= y; mov eax,dword ptr [x] xor eax,dword ptr [y] mov dword ptr [x],eax mov ecx,dword ptr [y] xor ecx,dword ptr [x] mov dword ptr [y],ecx mov edx,dword ptr [x] xor edx,dword ptr [y] mov dword ptr [x],edx 6 Mov + 3 Xor |
Re: Swap two variables using macro
the below logic also works as follows for swapping of nos without external variable introduction
Code:
main() |
| All times are GMT +5.5. The time now is 14:30. |