[Moved] Doubt on C programming (swapping of variables)

Status
Not open for further replies.

nthulasiram

Newbie level 2
Joined
Jul 28, 2014
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
14
Hi Friends,

how to swap two variables without using third variable without arithmetic operations and without bitwise operator. thanks for helping me.
 
Last edited by a moderator:

Re: Doubt on C programming (swapping of variables)

Let the two variables be a,b then code to swap them is
a=a+b;
b=a-b;
a=a-b;
(you can even do it using *,/ operators but it works only for integers.)
 
Re: Doubt on C programming (swapping of variables)

Let the two variables be a,b then code to swap them is
a=a+b;
b=a-b;
a=a-b;
(you can even do it using *,/ operators but it works only for integers.)


Those are arithmetic operations only na.( x = x*y; y = x/y; x = x/y; and a = b -~a-1; b = a+ ~b+1;
a = a+~b+1; and a = a^b; b = a^b; a = b^a

In the above, I mention without arithmetic and without 3rd variable without bitwise operator. how to swap two variables am looking for.
 

Use assembler and the stack,

Push a
Push b
Pop a
Pop b
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…