How to swap two numbers with the bitwise XOR operator in Java

1 Answer

0 votes
public class MyClass {
    public static void main(String args[]) {
        int x = 5;
        int y = 9;
    
        x ^= y; 
        y ^= x; 
        x ^= y;
    
        System.out.println(x + " " + y);
    }
}



/*
run:

9 5

*/

 



answered Mar 29, 2019 by avibootz

Related questions

1 answer 294 views
1 answer 339 views
1 answer 300 views
1 answer 276 views
1 answer 323 views
2 answers 375 views
...