answersLogoWhite

0


Best Answer

Swapping two values in a single statement is made possible through a series of three XOR/assign operations, alternating the operands.


Consider the following declarations:


int x = 0;

int y = 1;


The following statement will swap the values of x and y:


x ^= y ^= x ^= y; // swap



The same statement implemented as a function:


void swap(int &x, int &y){ x ^= y ^= x ^= y; }





User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

15y ago

Depending on your compiler, you may be able to use:

a = a+ b - (b = a);

This seems to work with gcc.

Using Microsoft's cl.exe (c++) gives the following output:

before swap: a = 5, b = 21

after swap: a = 5, b = 5

If this line does not work, another option would be to use your one line to make a function call, something like: void swap(int &x, int &y) { int temp = x; x = y; y = temp; }

int main() { int a = 10, b = 34; cout << "before swap: a = " << a << ", b = " << b << endl;

swap(a, b);

cout << "after swap: a = " << a << ", b = " << b << endl; return 0; }

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

a=a+b-(b=a); // Swaps variables a and b

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

temp=a; a=b; b=temp;

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How should you Swap 2 numbers in a single line?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you find the greatest of four numbers in a single line?

If the line is horizontal, the greatest is the furthest to the right.


How many numbers are there on a single number line?

A number line stretches out for infinity. So, infinity.


Do all real numbers lie on a single line in the complex plane?

Yes. Traditionally, this line is drawn horizontally, with positive numbers to the right, and negative numbers to the left.


Is the number nine a line symmetry?

no, only single digit numbers are 8,3,1, and 0


What number is parallel to 4 on a line graph?

There should be no parallel numbers on a line graph.


Do cable companies allow multiple numbers on one line?

Cable companies allow but one number per line at a single time. The way to add numbers is by purchasing an extra line, and you may have to purchase a new number as well.


What are the mixed numbers from 3 to 7?

You should used it in a number line


Can you swap 302 ford engine in to a 1980 cj 5 jeep 6 cil in line?

can you swap 302 ford engine in to 1980 cj 5 jeep 6 cil. in line?


Can you cross a double or single white line to make a left turn?

In the US, yes this is legal. A single or double white line signifies only that you should not change lanes.


What is the relationship between a number line and multiplacation?

Do you mean a line number and multiplication? A line number is a single number and a multiplication is two numbers multiplied. One and one is two. 1X2 is 3


How you can swap the axis of a chart?

Reflect the chart in the line y = x.


How many line are on a single staff?

there should be 5 lines and 4 spaces