1

I found this common solution :-

int a=10, b=20;    
a=a+b;
b=a-b;
a=a-b;

But what if a=2147483647 the largest value of an integer then probably a=a+b will not be feasible.

  • 2
    Then it won't work. Now think bitwise ops. – StoryTeller - Unslander Monica Oct 21 '13 at 16:46
  • Take both of variable as `unsigned lon long int`, help you to some extent :) – haccks Oct 21 '13 at 16:46
  • I know there is a very convoluted way to do this with bits. Unfortunately, I've been unable to find that recently. If you are worried about overflow, I'd use a large integer to store the value. Otherwise, there isn't much you can do with this approach. – Jonathan Wood Oct 21 '13 at 16:47
  • 8
    You do realize that the extra variable is going to be a register in the machine, and that using that extra variable will most probably be faster to execute and simpler to read, right? – David Rodríguez - dribeas Oct 21 '13 at 16:47
  • As far as I remember our professor who taught this to us said that on adding 1 to the largest +ve integer the answer will come to the smallest -ve integer. And on subtracting 1 from the smallest -ve integer the answer will come to the largest +ve integer. That is how he explained the concept of overflow. So that must not cause a problem here. – iluvthee07 Oct 21 '13 at 16:50
  • 3
    For C++: `using std::swap; swap(a, b);`. No extra variable in sight, *and* works for types other than `int`, and gets best performance when somebody has specialized `swap` for their particular type. – Jerry Coffin Oct 21 '13 at 16:51
  • 5
    @iluvthee07 - overflow and underflow of signed types have undefined behavior; the results that you describe are common, but not required. – Pete Becker Oct 21 '13 at 16:51

3 Answers3

14

How about using the std libs? ;)

std::swap(a,b);

Although you may also use the XORing algorithm but dont use it until you really have to.

The reason is well explained here:-

On modern CPU architectures, the XOR technique is considerably slower than using a temporary variable to do swapping. One reason is that modern CPUs strive to execute instructions in parallel via instruction pipelines. In the XOR technique, the inputs to each operation depend on the results of the previous operation, so they must be executed in strictly sequential order. If efficiency is of tremendous concern, it is advised to test the speeds of both the XOR technique and temporary variable swapping on the target architecture.

Although its too late but since you have not mentioned that you want build in functions or not hence the swap method is the easiest one.

However you may also try to use the XOR method(although check the above reference about its performance) like this:

a ^= b;
b ^= a;
a ^= b;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
6

The solution is:

a ^= b;
b ^= a;
a ^= b;

It works because x ^ x equals 0 for any value of x, and thus, x ^ y ^ x (in any order) equals y for any values of x and y. It's unlikely to be faster than just using a temporary though (unless you're programming for a CPU with high register contention and no pipelining ability).

Boann
  • 48,794
  • 16
  • 117
  • 146
  • XOR is actually a form of encryption and using it in this way is known as the one-time pad :) ... Since XOR is a reversible operation, your technically encrypting a with b, then setting b to a encrypted with b again, so b is then set to the decrypted a and vice versa with the variable b. – Riptyde4 Dec 03 '15 at 20:09
4

Try XORing as below.

a ^= b;
b ^= a;
a ^= b;
Siddhartha Ghosh
  • 2,988
  • 5
  • 18
  • 25