Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top