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 <iostream>

using namespace std;

int main(){
    
    int a=10;
    int b=20;
    
    cout << "First value is "<< a <<endl;
    cout << "Second value is "<< b <<endl;
    
    a = a ^ b;
    b = a ^ b;
    a = a ^ b;
    
    cout <<"== After Swapping ==" <<endl;
    cout << "First value is "<< a <<endl;
    cout << "Second value is "<< b <<endl;
	
    return 0;
    
}

Output :

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

Output :


                

Comments :