Swap ()

A

andrew.smith.cpp

Hello All :)
what is the right way to swap two values without using Temp (Or
Third variable).
1.) to use "^" operator
2.) to use this

a=a+b;
b=a-b;
a=a-b;


Which is the right Way ?????
 
R

red floyd

Hello All :)
what is the right way to swap two values without using Temp (Or
Third variable).
1.) to use "^" operator
2.) to use this

a=a+b;
b=a-b;
a=a-b;


Which is the right Way ?????

3. None of the above.

Consider:


void swap(int& a, int&b)
{
a=a+b;
b=a-b;
a=a-b;
}

int main()
{
int x = 7;
swap(x, x);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top