Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active Jul 5, 2020
Embed
What would you like to do?
public class SwapNumbers {
static void swap(int x, int y){
System.out.println("x: " + x + ", y: " + y);
//x : 4 => 0100
//y: 8 => 1000
x = x ^ y;
//x:1100, y: 1000
y = x ^ y;
//x:1100, y:0100
x = x ^ y;
//x:1000, y:0100
System.out.println("After swapping");
System.out.println("x: " + x + ", y: " + y);
}
public static void main(String[] args) {
int x = 4;
int y= 8;
swap(x,y);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment