Swapping two numbers using Bitwise Operator in C

This program swaps two numbers without using the third variable. It can be done by using the Bitwise operator

Source code :

#include <stdio.h>

int main(){
    
    int a=10;
    int b=20;
    
    printf("First value is %d Second value is %d",a,b );
    
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    
    printf("== After Swapping ==");
    printf("First value is %d Second value is %d",a,b );
	
    return 0;
    
}

Output :

First value is 10
Second value is 20
== After Swapping ==
First value is 20
Second value is 10

Output :


                

Comments :