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 147 views
1 answer 176 views
1 answer 142 views
1 answer 139 views
1 answer 159 views
2 answers 150 views
...