How to swap the value?


Good day everyone. I just want to ask a very simple question, how to swap the values of the two variable without declaring another variable?

Example:

int a=3,b=2;

The result: a=2,b=3.

how can i interchange the two numbers without using another variable..

can you tell me how and what are the ways to do the trick?

:) thanks for those who would answer.
Last edited on
The below will swap two values

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

int main()
{
	int a = 6; int b = 9;
	
	cout << "Old a="<<a<<" Old b="<<b<<endl;
	
	a=a+b;
	b=a-b;
	a=a-b;
	
	cout << "New a="<<a << " New b=" << b; 
}


Last edited on
Easy:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
using namespace std;

int main()
{
	int a = 6; int b = 9;

	cout << "Old a="<<a<<" Old b="<<b<<endl;

	a=b;
	b=6; //lol


	cout << "New a="<<a << " New b=" << b;
}
Using bitwise operations:

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
	int a=3, b=2;

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

	cout<<a<<endl<<b;

	return EXIT_SUCCESS;
}


EDIT: It would be interesting to check which method is faster, including using an third variable.
Last edited on
closed account (DSLq5Di1)
http://www.cplusplus.com/reference/algorithm/swap/
http://en.wikipedia.org/wiki/XOR_swap_algorithm
No need of that as i see. It can be done like this too.
Well, Visual C++ 2008 compiles the "normal" swap and the xor swap to identical machine code.

Compiling with /O2 (optimise for speed), the disassembly is (in all cases):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  1736: int main()
  1737: {
  1738: 	Swap();
00401000 A1 40 20 40 00   mov         eax,dword ptr [__imp_std::endl (402040h)] 
00401005 50                 push        eax  
00401006 6A 03            push        3    
00401008 8B C8            mov         ecx,eax 
0040100A 51                 push        ecx  
0040100B 8B 0D 44 20 40 00 mov         ecx,dword ptr [__imp_std::cout (402044h)] 
00401011 6A 02            push        2    
00401013 FF 15 3C 20 40 00 call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40203Ch)] 
00401019 8B C8            mov         ecx,eax 
0040101B FF 15 38 20 00 00 call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] 
00401021 8B C8            mov         ecx,eax 
00401023 FF 15 3C 20 40 00 call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (40203Ch)] 
00401029 8B C8            mov         ecx,eax 
0040102B FF 15 38 20 40 00 call        dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] 
  1739: 
  1740: 	return 0;
00401031 33 C0          xor         eax,eax 
  1741: }
00401033 C3               ret  


For

1
2
3
4
5
6
7
8
void Swap()
{
	int a=3, b=2;
	int t = b;
	b = a;
	a = t;
	cout<<a<<endl<<b<<endl;
}


1
2
3
4
5
6
7
8
void Swap()
{
	int a=3, b=2;
	a^=b;
	b^=a;
	a^=b;
	cout<<a<<endl<<b<<endl;
}


and (for completeness)

1
2
3
4
5
6
void Swap()
{
	int a=3, b=2;
	std::swap(a, b);
	cout<<a<<endl<<b<<endl;
}


Note that the function is being inlined.
Last edited on
with xor you need to check if the 2 values are identical before you swap
closed account (DSLq5Di1)
wikipedia wrote:
Most modern compilers can optimize away the temporary variable in the naive swap, in which case the naive swap uses the same amount of memory and the same number of registers as the XOR swap and is at least as fast, and often faster. The XOR swap is also much less readable, and can be completely opaque to anyone who isn't already familiar with the technique.

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.

Prefer std::swap over any manual implementation.
Intrexa - would it be better to state that you need to check that the two variables are distinct?

From the point of view of the swap logic, it doesn't matter what the two values are. But if you store them in the same variable it just zeroes it.

The stl algorithm checks that the addresses of the two variable are different before carrying on.
thanks for the help ..

i have also thought about this method,
a=a+b;
b=a-b;
a=a-b;

my classmate already did analyzed it, i was just wondering if there is another way how to swap the two numbers without using any lib. func. or using a third variable. THANKS GUYS, see ya..
Topic archived. No new replies allowed.