Skip to content

Instantly share code, notes, and snippets.

@charlespunk
Created March 15, 2013 15:53
Show Gist options
  • Save charlespunk/5170880 to your computer and use it in GitHub Desktop.
Save charlespunk/5170880 to your computer and use it in GitHub Desktop.
Write a function to swap a number in place (that is, without temporary variables).
public static void swap(int a, int b){
a = a - b;
b = a + b;
a = b - a;
System.out.println("a is " + a);
System.out.println("b is " + b);
}
public static void swap(int a, int b){
a = a ^ b;
b = a ^ b;
a = a ^ b;
System.out.println("a is " + a);
System.out.println("b is " + b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment