Skip to content

Instantly share code, notes, and snippets.

@wszdwp
Created September 22, 2013 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wszdwp/6663231 to your computer and use it in GitHub Desktop.
Save wszdwp/6663231 to your computer and use it in GitHub Desktop.
Cracking the interivew 19.1
//Java is not working ??? pass by value??
public class Question191
{
public static void swap(int a, int b) {
a = a ^ b;
b = a ^ b;
a = a ^ b;
}
public static void main(String[] args) {
int a = 2;
int b = 3;
//Question191 test1 = new Question191();
System.out.println("a, b = " + a +", " + b);
swap(a, b);
System.out.println("After swap, a, b = " + a +", " + b);
}
}
//C++ code is working
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a = 2;
int b = 3;
a = a - b;
b = b + a;
a = b - a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment