How to swap two numbers with the bitwise XOR operator in C++

1 Answer

0 votes
#include <iostream>

int main() {
    int x = 5;
    int y = 9;
     
    x ^= y; 
    y ^= x; 
    x ^= y;
     
    std::cout << "x = " << x << " y = " << y;
}
 
 
/*
run:
 
x = 9 y = 5
 
*/

 



answered Mar 28, 2019 by avibootz
edited Apr 10, 2024 by avibootz

Related questions

1 answer 147 views
1 answer 176 views
1 answer 142 views
1 answer 139 views
1 answer 139 views
2 answers 150 views
...