Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Swap two variables using macro (http://www.go4expert.com/articles/swap-variables-using-macro-t1732/)

Ziaur Rahman 26Oct2006 18:42

Swap two variables using macro
 
The code to swap two variables using macro expansions

Code: C

#include<stdio.h>
#include<conio.h>
#define SWAPE(x,y) int t;t=x;x=y;y=t;
main()
{
    int a,b;
   
    printf("\n Enter two number");
    scanf("%d%d",&a,&b);
    printf("\n Before swaping the Value of a=%d and b=%d",a,b);
    SWAPE(a,b);
    printf("\n After swap value of a=%d and b=%d",a,b);
    return 0;
}


nicenaidu 2Nov2006 13:35

Swap two numbers without using temp variable
 
//Code for swapping two numbers without using temp variable
Code: CPP

#include <iostream>
using namespace std;
int main ()
{
  int a = 10;
  int b = 5;

  cout << "before swap: a = " << a << " b = " << b << endl;
 
  //Swapping numbers without using a temp var : method1
  a = a ^ b;
  b = b ^ a;
  a = a ^ b;

  cout << "after swap using method1: a = " << a << " b = " << b << endl;

  //Swapping numbers without using a temp var : method2
  a = a + b;
  b = a - b;
  a = a - b;
  cout << "after swap using method2: a = " << a << " b = " << b << endl;

  return 0;
}


aurnk 22Dec2006 14:42

Re: Swap two variables using macro
 
better method is
usage of
#define swap(a,b) a^=b^=a^=b;

aurnk 22Dec2006 14:43

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.

Peter_APIIT 12Apr2007 11:15

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.

Vikrant Singh 24May2007 15:43

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.

oleber 25May2007 13:30

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

template <class TYPE>
void swamp(TYPE &var1, TYPE &var2) {
    TYPE var_temp = var1;
    var1 = var2;
    var2 = var_temp;
}


Vikrant Singh 25May2007 18:59

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;

oleber 25May2007 20:04

Re: Swap two variables using macro
 
Correct to :p

Code:

    #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;}

Other wise you get problems when using diferent types

Code: CPP

#include <cstdio>
    #include <iostream>
   
    using namespace std;
   
    #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;}
   
    int main () {
       
        char v1 = 'a';
        char v2 = 'b';   
        SWAP (v1, v2);
        cout << v1 << "   " << v2 <<"\n";
   
        float f1 = 45;
        float f2 = 123;
        SWAP (f1, f2);
        cout << f1 << "   " << f2 <<"\n";
    }


keith12125 4Jul2007 15:39

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?


All times are GMT +5.5. The time now is 03:26.