[algogeeks] Re: swap objects without temp variable

2012-11-19 Thread Don
*Sigh* On Nov 19, 6:24 am, Gaurav Gulzar wrote: > one liner > a^=b^=a^=b > > On Mon, Nov 19, 2012 at 4:30 PM, abhinav gupta wrote: > > > > > > > > > we need to use + and - as operator overloading. so, that i will work f9 > > for ur objects. > > > so make operator overloading for + and - opera

Re: [algogeeks] Re: swap objects without temp variable

2012-11-19 Thread Gaurav Gulzar
one liner a^=b^=a^=b On Mon, Nov 19, 2012 at 4:30 PM, abhinav gupta wrote: > we need to use + and - as operator overloading. so, that i will work f9 > for ur objects. > > so make operator overloading for + and - operator. > > > On Sat, Nov 17, 2012 at 9:12 PM, AISHWARYA KUMAR wrote: > >

Re: [algogeeks] Re: swap objects without temp variable

2012-11-19 Thread abhinav gupta
we need to use + and - as operator overloading. so, that i will work f9 for ur objects. so make operator overloading for + and - operator. On Sat, Nov 17, 2012 at 9:12 PM, AISHWARYA KUMAR wrote: > > in one line > a=a^b^(b=a) ; > > -- > > > -- *Thanks and Regards,* Ab

[algogeeks] Re: swap objects without temp variable

2012-11-19 Thread AISHWARYA KUMAR
in one line a=a^b^(b=a) ; --

[algogeeks] Re: swap objects without temp variable

2012-11-15 Thread Sarath
That's right! On Monday, November 5, 2012 2:02:43 AM UTC+5:30, manish wrote: > > Swapping two objects (not integers/chars),without using temp...? > my solution is using xor operation..is that right and ny other solutions ? -- You received this message because you are subscribed to the Google Gro

[algogeeks] Re: swap objects without temp variable

2012-11-15 Thread DHAARAA
Can anyone please help me to understand (with example) that how those solutions work, if they are OBJECTS? On Sunday, November 4, 2012 3:32:43 PM UTC-5, manish wrote: > > Swapping two objects (not integers/chars),without using temp...? > my solution is using xor operation..is that right and ny ot

[algogeeks] Re: swap objects without temp variable

2012-11-14 Thread Dave
@Don: This can be easily fixed, as there is no need to swap equal values, so: void swap(int &a, int &b) { if( a != b ) { a ^= b; b ^= a; a ^= b; } } On Monday, November 5, 2012 10:41:42 AM UTC-6, Don wrote: > Note that most of these methods fail if you try t

[algogeeks] Re: swap objects without temp variable

2012-11-13 Thread Don
Dave is right. The code is undefined, which means that a valid compiler could produce code which makes monkeys fly out of your nose. On Nov 13, 12:38 am, Dave wrote: > @Shivam: Your one-line "solution" violates the sequence point rule. Hence, > it is non-standard, and the result is compiler depen

Re: [algogeeks] Re: swap objects without temp variable

2012-11-05 Thread Umer Farooq
But how is that going to work for objects? On Mon, Nov 5, 2012 at 6:43 AM, Ashok Varma wrote: > Try this: a = a + b - (b = a); //single line code to swap > > > > > On Mon, Nov 5, 2012 at 4:53 AM, Dave wrote: > >> @Manish: Sure. >> >> a = a + b; >> b = a - b; >> a = a - b; >> >> In 2-s complemen

[algogeeks] Re: swap objects without temp variable

2012-11-05 Thread Don
Note that most of these methods fail if you try to swap an item with itself. For example, swap(a[i], a[j]) will fail if i==j and swap is implemented as void swap(int &a, int&b) { a ^= b; b ^= a; a ^= b; } Don On Nov 4, 3:32 pm, manish wrote: > Swapping two objects (not integers/chars)

Re: [algogeeks] Re: swap objects without temp variable

2012-11-04 Thread Ashok Varma
Try this: a = a + b - (b = a); //single line code to swap On Mon, Nov 5, 2012 at 4:53 AM, Dave wrote: > @Manish: Sure. > > a = a + b; > b = a - b; > a = a - b; > > In 2-s complement arithmetic, it works even if a + b overflows. > > Dave > > On Sunday, November 4, 2012 2:32:43 PM UTC-6, manish

[algogeeks] Re: swap objects without temp variable

2012-11-04 Thread Dave
@Manish: Sure. a = a + b; b = a - b; a = a - b; In 2-s complement arithmetic, it works even if a + b overflows. Dave On Sunday, November 4, 2012 2:32:43 PM UTC-6, manish wrote: > Swapping two objects (not integers/chars),without using temp...? > my solution is using xor operation..is tha