c++ swap programe

Mar 14, 2010 at 8:43am
closed account (iGAMDjzh)
i need c++ swaping programe without using temp. variable.
please i need help.
Mar 14, 2010 at 9:10am
What type of variable can you use? Did you do something already upon we can build something together? Do you know how to define functions?

Maikel
Mar 14, 2010 at 10:01am

If you have to swap numbers then this will do the job.

int a = 5;
int b = 7;

//swapping
a^=b;
b^=a;
a^=b;

cout << "a: " << a << endl;//7
cout << "b: " << b << endl;//5
Mar 15, 2010 at 8:29am
You can also use simple addition and subtraction method.
1
2
3
4
5
6
7
8
9
10
int a = 5;
int b = 7;

//swapping
a=a+b;
b=a-b;
a=a-b;

cout << "a: " << a << endl;//7
cout << "b: " << b << endl;//5 
Mar 15, 2010 at 10:58am
tajendra wrote:
You can also use simple addition and subtraction method.

If a or b is too big, you get an integer overflow, which is "undefined behaviour" in pure C++ terminology. (But practically, it should work with almost all environments nowadays).

Also, both versions may cause havoc if a and b are of different integral types.
Mar 16, 2010 at 12:19pm
"Also, both versions may cause havoc if a and b are of different integral types."
It is not wise to swap numbers of different types.
Mar 16, 2010 at 12:25pm
Don't you need a third variable to store them as they move?

1
2
3
4
int x;
x = a;
a = b;
b = x;


where x is the third "box" which starts empty.
Mar 17, 2010 at 7:11am
closed account (1yR4jE8b)
Not if you use XOR

1
2
3
a^=b;
b^=a;
a^=b;


let's say that a = 4 and b = 10

4 in binary is 0100
10 in binary is 1010

a ^= b assigns a to a XOR b so 0100 XOR 1010 = 1110 is 14
b ^= a assign b to a XOR b so 1110 XOR 10101 = 0100 is 4

finally, a ^= b assigns a to a XOR b so 1110 XOR 0100 = 1010 is 10

so now a = 10 and b = 4

Doing swaps this way does not require the use of a temporary variable.

Another cool little bitshifting trick is if you need to zero a variable, just XOR it with itself
1
2
3
4
5

int a = 9999;
a ^= a
cout << boolalpha << (a == 0);

Last edited on Mar 17, 2010 at 7:12am
Mar 17, 2010 at 7:47am
Another cool little bitshifting trick is if you need to zero a variable, just XOR it with itself


Except of the cocktail-party-bragging ability.. what is this usefull for? :-D

I'd rather turn it around: If you happen to recognize, that someone XOR's a variable with itself, just set it to 0 for better readability instead.

Ciao, Imi.
PS: Don't even think the word "performance" here, please. ;-)
Mar 17, 2010 at 8:29am
closed account (1yR4jE8b)
Well, I wouldn't say it's particularly usefull. A compiler would probably do that anyway.

It's just something that I picked up during my Assembly Language Programming course in University and the prof (who is like 1000 years old and still programs for MS DOS), was obsessed with efficiency. So we HAD to do things like this in our assignments or we'd lose marks.

so instead of writing

mov ax, 0

we HAD to write

xor ax, ax

or we'd lose marks.

So after 4 monthes of anal retentive obsession over this, quite a few of his optimization quirks have just stuck

I would NEVER write a ^= a in a higher level language code.

I actually did the XOR swap in a Java course once, and the marker had no idea why it worked lol
:P
Mar 17, 2010 at 8:34am
A compiler would probably do that anyway.

Or turn it around, if "mov ax,0" is faster *g*.


heh.. yea, well.. reminds me.. Some processors have hard-coded zero registers so there is "nothing faster than setting something to 0" anyway. (But I'm not so sure about this. maybe that's something for another cocktail party)

Ciao, Imi.
Topic archived. No new replies allowed.