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 213 views
1 answer 254 views
1 answer 200 views
1 answer 195 views
1 answer 239 views
2 answers 260 views
...