Search This Blog

Program to Swap Numbers with Bitwise (EXOR) Operators

Here is a c program to swap two numbers. This method of swapping does not use a third (temporary) variable. Instead, swapping is done by bitwise operation. We perform bitwise EXOR operations on the two numbers. The program is as follows:

#include<stdio.h>
void main()
{ int x,y; x=200; y=140; printf("\nBefore swapping x=%d,y=%d",x,y); x=x^y; y=x^y; x=x^y; printf("\nAfter swapping x=%d y=%d",x,y); }

No comments: