We can write solution for this problem in two way i.e.
- Call by reference
- Call by value
Here is the C program using method 1 i.e. call by reference to swap two numbers using bit-wise operator.
/* Call by Reference:To Swap two Numbers using Bit-wise operators */
#include<stdio.h>
/* swap () to swap two numbers using bitwise operator */
void swap ( int *numFirst, int *numSecond )
{
*numFirst = *numFirst ^ *numSecond ;
*numSecond = *numFirst ^ *numSecond ;
*numFirst = *numFirst ^ *numSecond ;
}
int main ()
{
int numOne = 15;
int numTwo = 12;
printf( "\nBefore swapping, numOne %d and numTwo %d\n", numOne, numTwo );
/* Call by reference to swap ( ) */
swap ( &numOne, &numTwo );
printf( "\nAfter swapping, numOne %d and numTwo %d\n", numOne, numTwo );
return 0;
}
Here is the C program using method 2 i.e. call by value to swap two numbers using bit-wise operator.
/* Call by value : To Swap two Numbers using Bit-wise operators */
#include<stdio.h>
void swap ( int numFirst, int numSecond )
{
numFirst = numFirst ^ numSecond ;
numSecond = numFirst ^ numSecond ;
numFirst = numFirst ^ numSecond ;
printf( "\nAfter swapping, numOne %d and numTwo %d\n", numFirst, numSecond );
}
int main ()
{
int numOne = 15;
int numTwo = 12;
printf (" \nBefore swapping, numOne %d and numTwo %d\n", numOne, numTwo );
/* Call by value to swap ( ) */
swap ( numOne, numTwo );
return 0;
}
To contribute :
” If you like Advance Computing and would like to contribute, you can mail your article to “computingadvance@gmail.com”. You will get credit as your name , email id, designation with article on this blog. “