tag:blogger.com,1999:blog-2349434231090974738.post2042196431114566691..comments2023-10-24T19:15:52.177+01:00Comments on Advanced C++ with Examples: Swap two variables without using third and in one lineZahid Ghadialyhttp://www.blogger.com/profile/11618208626682295272noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-2349434231090974738.post-18569279850578645302012-01-25T23:43:17.293+00:002012-01-25T23:43:17.293+00:00I make it even more interesting
the variables are...I make it even more interesting <br />the variables are of 8bit n unsignedAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-2964713880135905282012-01-24T09:34:36.978+00:002012-01-24T09:34:36.978+00:00a=a+b-(b=a) ;a=a+b-(b=a) ;Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-46395682050296983072012-01-04T18:01:55.171+00:002012-01-04T18:01:55.171+00:00This does not seem like a relevant interview quest...This does not seem like a relevant interview question to me. I'd prefer my hires NOT use this because it's so cryptic. The ability to write clean code and express your thoughts concisely would be my number one criteria, not esoteric bit-wise operations that could be Googled in 30 seconds.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-34510587153714568052011-08-10T09:39:58.987+01:002011-08-10T09:39:58.987+01:00@ //b=(a+b)-(a=b); - This should work but doesnt, ...@ //b=(a+b)-(a=b); - This should work but doesnt, why?<br /><br />this should be <br />b=(a-b)+(a=b);Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-69489623008743187862011-03-18T07:04:37.942+00:002011-03-18T07:04:37.942+00:00ap: Which compiler are you using?
It also depends...ap: Which compiler are you using?<br /><br />It also depends on how and where you use these statements. Did you use my program as it is and it complained?Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-20591047596812334032011-03-18T06:35:19.179+00:002011-03-18T06:35:19.179+00:00a^=b^=a^=b;
a =((a = a + b) - (b = a - b));
a = ((...a^=b^=a^=b;<br />a =((a = a + b) - (b = a - b));<br />a = ((a = a * b) / (b = a / b));<br /><br /><br />All of them cause Undefined Behaviour - variable is modified twice between consecutive sequence points.aphttps://www.blogger.com/profile/10456620306952874820noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-58026622710723918712011-03-12T16:38:55.440+00:002011-03-12T16:38:55.440+00:00ok sir i'll keep that in mind while makin futu...ok sir i'll keep that in mind while makin future programs...<br />thanks for for the feedbackvaibhavnoreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-66984803740584769612011-03-12T07:07:13.583+00:002011-03-12T07:07:13.583+00:00Vaibhav, your approach is fine as its the same as ...Vaibhav, your approach is fine as its the same as approach 2:<br />a =((a = a + b) - (b = a - b));<br />I generally tend to discourage people writing it this way as it can make the code unreadable. Otherwise its fine.Zahid Ghadialyhttps://www.blogger.com/profile/11618208626682295272noreply@blogger.comtag:blogger.com,1999:blog-2349434231090974738.post-22707847525802783612011-03-12T04:03:34.331+00:002011-03-12T04:03:34.331+00:00sir please have a look at my prog:
#include
using...sir please have a look at my prog:<br /><br />#include<br />using namespace std;<br /><br />int main()<br />{<br />int a=6,b=2;<br /><br />cout<<"original:"<<endl<<"a="<<a<<endl<<"b="<<b<<endl;<br /><br />a-=(b=(a+=b)-b);<br /><br />cout<<"new values:"<<endl<<"a="<<a<<endl<<"b="<<b;<br /><br />getchar();<br />return 0;<br />}vaibhav1302https://www.blogger.com/profile/15945518763312009671noreply@blogger.com