Tuesday, 17 March 2015

C Program to swap using bitwise XOR

#include <stdio.h>
 
int main()
{
  int x, y;
 
  scanf("%d%d", &x, &y);
 
  printf("Before Swapping x = %d\ny = %d\n", x, y);
 
  x = x ^ y;
  y = x ^ y;
  x = x ^ y;
 
  printf("After Swqpping x = %d\ny = %d\n", x, y);
 
  return 0;
}
 
Output of Program

Before Swapping x=10
y=50

After Swapping x=50
y=10

No comments:

Post a Comment