Jul 28, 2014 #1 N 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: Jul 28, 2014
Hi Friends, how to swap two variables without using third variable without arithmetic operations and without bitwise operator. thanks for helping me.
Jul 28, 2014 #2 Y Y.SAI SARASWATHI Member level 4 Joined Apr 27, 2014 Messages 74 Helped 8 Reputation 16 Reaction score 8 Trophy points 8 Activity points 469 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.)
Jul 29, 2014 #3 N nthulasiram Newbie level 2 Joined Jul 28, 2014 Messages 2 Helped 0 Reputation 0 Reaction score 0 Trophy points 1 Activity points 14 Re: Doubt on C programming (swapping of variables) Y.SAI SARASWATHI said: 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.) Click to expand... 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.
Re: Doubt on C programming (swapping of variables) Y.SAI SARASWATHI said: 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.) Click to expand... 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.
Aug 24, 2014 #4 B btbass Advanced Member level 5 Joined Jul 20, 2001 Messages 1,896 Helped 438 Reputation 880 Reaction score 288 Trophy points 1,363 Location Oberon Activity points 12,887 Use assembler and the stack, Push a Push b Pop a Pop b