Wednesday, September 11, 2013

Tagged Under:

Swap Variables using Bit Wise Operator

Share


Swap variables using bit-wise EX-OR operator in C.

Bit-wise operators carry operations on Bits at individual level. There are a number of methods to swap two variables in C such as temp variables method, 2 variable method. Now this is an interesting method to swap variable.


Swap 2 variables using bit-wise EX-OR operators in C.

#include<stdio.h>
#include<conio.h>
  void main()
   {
     int a,b;
     clrscr();
     printf("\n Enter first variable : ");
     scanf("%d",&a);
     printf("\n Enter second variable : ");
     scanf("%d",&b);
     printf("\n Variables before Swapping \n");
     printf("a= %d, b=%d ",a,b);
     a^=b^=a^=b;
     printf("\n Variables after Swapping \n");
     printf("a= %d, b=%d ",a,b);
     getch();
   }

If having any problem regarding programming in C , JAVA, PHP feel free to contact !