C Program Swap Two Variables using Bitwise XOR

By | March 6, 2020

C Program Swap Two Variables using Bitwise XOR

Swap Two Variables using Bitwise XOR: C Program

#include <stdio.h>
int main()
{
  
  int n1,n2;

  printf("Enter two numbers to swap values using Bitwise XOR Operator\n");
  scanf("%d%d", &n1, &n2);

  printf("Before swapping: n1=%d and n2=%d", n1,n2);
  
  n1 = n1 ^ n2;
  n2 = n1 ^ n2;
  n1 = n1 ^ n2;
  
  
  printf("\nAfter  swapping: n1=%d and n2=%d", n1,n2);
  

  return 0;
}

Output

Enter two numbers to swap values using Bitwise XOR Operator
50
100
Before swapping: n1=50 and n2=100
After swapping: n1=100 and n2=50
Press Enter to return to Quincy…

Leave a Reply

Your email address will not be published. Required fields are marked *