1

Possible Duplicate:
How does XOR variable swapping work?
Swap the values of two variables without using third variable

How can I write a C code to swap 2 integer variables without using any extra variable?

Community
  • 1
  • 1

2 Answers2

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

use XOR ( ^ ) operator
you can do it with adding:

x = x + y;
y = x - y;
x = x - y;

but I think XOR is the best way because it's faster.
This is a bitwise operator like & and |.
1^1 == 0
0^0 == 0
1^0 == 1
0^1 == 1
You can read this for more information

shift66
  • 11,760
  • 13
  • 50
  • 83
  • yes. ^ converts to XOR command of assembler which is much faster then ADD. and + converts to ADD – shift66 Feb 14 '12 at 08:24
  • Actually all bitwise operators are fast. bitwise and, or, shifts... – shift66 Feb 14 '12 at 08:26
  • 2
    @Ademiban: Please read this answer if you're interested in speed: http://stackoverflow.com/a/249469/893 – Greg Hewgill Feb 14 '12 at 08:29
  • 2
    @Ademiban: I would be disappointed if my CPU couldn't do an integer addition as fast as an XOR. – Oliver Charlesworth Feb 14 '12 at 09:03
  • @OliCharlesworth Addition my overflow and thus lead to undefined behavior for signed integral types. That's one more reason to prefer `^`. – fredoverflow Feb 14 '12 at 10:11
  • @FredOverflow: XOR also has potential UB for signed types, if you generate a negative zero and the implementation doesn't support them ;-) – Steve Jessop Feb 14 '12 at 12:19
  • @FredOverflow: no. I mean, it's true but I don't care, in practice I'd write the code anyway because I disbelieve that my code will ever be run on an implementation that isn't 2's complement. I just find it amusing how difficult the C and C++ standards make it to do anything with signed types that's strictly conforming. – Steve Jessop Feb 14 '12 at 14:38
0
a = a^b;
b = a^b;
a = a^b;

Just use XOR operator for your purposes. Just for explanation

XOR (Exclusive Or) This operation is performed between two bits (a and b). The result is 1 if either one of the two bits is 1, but not in the case that both are. There for, if neither or both of them are equal to 1 the result is 0.

For example

a=5; //011
b=6; //100

a = a^b; //a=111
b = a^b; //b=011
a = a^b; //a=100
Community
  • 1
  • 1
Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
  • As a side note, I have had strange bugs in legacy code where the previous developer used this to swap two numbers because it was "faster". He did not however anticipate that sometimes it would be passed the same number for both `a` and `b`, leading to them being zeroed out. Why needing to be faster on something that is run once on program start-up and takes a total time of a few milliseconds I will never know. – r_ahlskog Feb 14 '12 at 09:12
  • 1
    @r_ahlskog: this does actually work for equal values, it fails when `a` and `b` refer to the same memory location. You're right though, it's a terrible idea to actually use this. I'm not sure I even believe that it's faster than using a short-lived variable. – Steve Jessop Feb 14 '12 at 12:22
  • Whops, why yes, that was what I intended to write. The code was part of an encryption algorithm of unknown origin. It broke on compiler change from Visual C++6 to Visual Studio 2008, on analysis of it I found it had a bias towards producing zeroes due to the xor swap method used. – r_ahlskog Feb 14 '12 at 14:09