Skip to content

Instantly share code, notes, and snippets.

@thmain
Last active July 5, 2020 09:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thmain/075ad647f4cafe0df689c2286e197a66 to your computer and use it in GitHub Desktop.
Save thmain/075ad647f4cafe0df689c2286e197a66 to your computer and use it in GitHub Desktop.
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