[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

[algogeeks] Re: Swap the LSB and MSB within themself of given no.

2010-11-22 Thread Dave
@Coolfrog$: It will be instructive if you do it for yourself. Perhaps with practice you can work through your confusion. Have you worked out the bit patterns of those hex constants? They are crucial to understanding Gene's algorithm. -- Dave On Nov 22, 6:55 am, "coolfrog$" wrote: > @dave > plz ru

Re: [algogeeks] Re: Swap the LSB and MSB within themself of given no.

2010-11-22 Thread coolfrog$
@dave plz run gene's code for input 0xAD...or send me some link of bitwise programing which involve simultaneous many opearation... like above... i am always confused with bitwise programing... On Sun, Nov 21, 2010 at 11:11 PM, Dave wrote: > @Coolfrog$: Don't forget the bitwise logical produc

Re: [algogeeks] Re: Swap the LSB and MSB within themself of given no.

2010-11-21 Thread DIPANKAR DUTTA
x=((x>>3)&(0x11)|(a<<3)& 0x88 |(a>>1)& 0x44 | (a<<1) && 0x22) On Sun, Nov 21, 2010 at 11:11 PM, Dave wrote: > @Coolfrog$: Don't forget the bitwise logical products. What is the bit > patterns in those hexadecimal constants? Work out the whole example > and you will see how it works. -- Dave > >

[algogeeks] Re: Swap the LSB and MSB within themself of given no.

2010-11-21 Thread Dave
@Coolfrog$: Don't forget the bitwise logical products. What is the bit patterns in those hexadecimal constants? Work out the whole example and you will see how it works. -- Dave On Nov 21, 8:21 am, "coolfrog$" wrote: > @gene > plz explain .. what is going on... by taking example. i am unable

Re: [algogeeks] Re: Swap the LSB and MSB within themself of given no.

2010-11-21 Thread coolfrog$
@gene plz explain .. what is going on... by taking example. i am unable to run a test case 1. x=0xAD (1010 1101) 2. x<<1 ===>01011010 | x>>1 >01010110 x =0100 how we will get answer as ( 0101 1011).?? On Sun, Nov 21, 2010 at 9:59 AM, Gene

[algogeeks] Re: Swap the LSB and MSB within themself of given no.

2010-11-20 Thread Gene
if the input is in unsigned char x, then x = ((x << 1) & 0xAA) | ((x >> 1) & 0x55) x = ((x << 2) & 0xCC) | ((x >> 2) & 0x33) On Nov 20, 10:41 pm, Divesh Dixit wrote: > assuming all are 8bit no. > input = 0x46  (0100   0110) >  output = 0x26 ( 0010  0110 ) > input = 0x75 (0111  0101) > output = 0

[algogeeks] Re: Swap the LSB and MSB within themself of given no.

2010-11-20 Thread Gene
In C, unsigned char x; ... x = (x << 1) | (x >> 1); x = (x << 2) | (x >> 2); On Nov 20, 10:41 pm, Divesh Dixit wrote: > assuming all are 8bit no. > input = 0x46  (0100   0110) >  output = 0x26 ( 0010  0110 ) > input = 0x75 (0111  0101) > output = 0xFC (1110  1010 ) > > Algorithm..??? -- You re

Re: [algogeeks] Re: Swap the bits

2010-06-23 Thread manisha nandal
ohh :( On 6/23/10, mohit ranjan wrote: > @Manisha > > "swap every two bits" > > > -Mohit > > > > On Wed, Jun 23, 2010 at 4:56 PM, manisha nandal > wrote: > >> char a=10 01 11 01 >> a = a ^ ~ ( 0 ) >> //now a is 01 10 00 10 >> >> -- >> You received this message because you are subscribed to the G

Re: [algogeeks] Re: Swap the bits

2010-06-23 Thread mohit ranjan
@Manisha "swap every two bits" -Mohit On Wed, Jun 23, 2010 at 4:56 PM, manisha nandal wrote: > char a=10 01 11 01 > a = a ^ ~ ( 0 ) > //now a is 01 10 00 10 > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group,

[algogeeks] Re: Swap the bits

2010-06-23 Thread Dave
Manisha, this is not the desired result. -- Dave On Jun 23, 6:26 am, manisha nandal wrote: > char a=10 01 11 01 > a = a ^ ~ ( 0 ) > //now a is 01 10 00 10 -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to

Re: [algogeeks] Re: Swap the bits

2010-06-23 Thread manisha nandal
char a=10 01 11 01 a = a ^ ~ ( 0 ) //now a is 01 10 00 10 -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algoge...@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@go

[algogeeks] Re: Swap the bits

2010-06-21 Thread Dave
Let me explain, supposing that you haven't really tried to understand the code. The first logical product picks out bits 1, 3, 5, and 7 and shifts them 1 position to the right. The second logical product picks out bits 0, 2, 4, and 6 and shifts them 1 position to the left. Then just or the two sets

Re: [algogeeks] Re: Swap the bits

2010-06-21 Thread Amit Jaspal
@ Dave would u plz bother to discuss how do u arrive at this formula? On Mon, Jun 21, 2010 at 10:11 PM, Dave wrote: > Not hard at all: > > y = ((x & 0xAA) >> 1) | ((x & 0x55) << 1) > > Dave > > On Jun 21, 7:07 am, amit wrote: > > Given a byte, write a code to swap every two bits. [Using bit > >

Re: [algogeeks] Re: Swap the bits

2010-06-21 Thread mohit ranjan
oops so sleek and simple :) Mohit Ranjan On Mon, Jun 21, 2010 at 10:11 PM, Dave wrote: > Not hard at all: > > y = ((x & 0xAA) >> 1) | ((x & 0x55) << 1) > > Dave > > On Jun 21, 7:07 am, amit wrote: > > Given a byte, write a code to swap every two bits. [Using bit > > operators] Eg: Input: 10

[algogeeks] Re: Swap the bits

2010-06-21 Thread Dave
Not hard at all: y = ((x & 0xAA) >> 1) | ((x & 0x55) << 1) Dave On Jun 21, 7:07 am, amit wrote: > Given a byte, write a code to swap every two bits. [Using bit > operators] Eg: Input: 10 01 11 01 Output: 01 10 11 10 -- You received this message because you are subscribed to the Google Groups

[algogeeks] Re: swap every two bits in the byte..

2009-09-05 Thread Ajith G
a=((number & 01010101)<<1) b=((number & 10101010)>>1) a OR b the bits will be exchanged . i hope you understood. On Sat, Sep 5, 2009 at 4:45 AM, Pramod Negi wrote: > i guess > num = ((num&0xAA)>>1) | ((num&0x55)<<1)) > will work > > Negi > > > On Sat, Sep 5, 2009 at 2:08 PM, Gokul wrote: >

[algogeeks] Re: swap every two bits in the byte..

2009-09-05 Thread Pramod Negi
i guess num = ((num&0xAA)>>1) | ((num&0x55)<<1)) will work Negi On Sat, Sep 5, 2009 at 2:08 PM, Gokul wrote: > > how ll u swap every two bits in the a byte??? can anyone help me??? > for eg. > consider a byte as input... > 10111010 > > output should be > 01110101 > > it exactly swap the two bit

[algogeeks] Re: swap

2006-03-09 Thread Michael Ageeb
one linea^=b^=a^=bOn 3/9/06, hemu <[EMAIL PROTECTED]> wrote: A different way ( or operators) but underlined logic is same as that ofXORA=  (  A &  ~ B )  |  ( ~A &  B)B=   (  A &  ~ B ) |  ( ~A &  B)A=  (  A &  ~ B )  |  ( ~A &  B) Don't send me any attachment in Micro$oft (.DOC, .PPT)

[algogeeks] Re: swap

2006-03-09 Thread hemu
A different way ( or operators) but underlined logic is same as that of XOR A= ( A & ~ B ) | ( ~A & B) B= ( A & ~ B ) | ( ~A & B) A= ( A & ~ B ) | ( ~A & B) --~--~-~--~~~---~--~~ You received this message because you are subscribed to

[algogeeks] Re: swap

2006-03-08 Thread manu jose
THat is correct . .On 3/8/06, Dhyanesh <[EMAIL PROTECTED]> wrote: Although perfectly valid, the first and third are subject to integer overflow. The second one is the safest of all. On 3/8/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: -- Manu Jose,mob :09844467453E-mail : [EMAIL PROTECTED]

[algogeeks] Re: swap

2006-03-08 Thread Dhyanesh
Although perfectly valid, the first and third are subject to integer overflow. The second one is the safest of all.On 3/8/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Grou

[algogeeks] Re: swap

2006-03-08 Thread zavandi
On 3/8/06, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > > I could find only 3 > > 1) a= a+b;b=a-b;a=a-b; > 2) a= a^b;b=a^b;a=a^b; > 3) a= a*b;b=a/b;a=a/b; > > Did anyone get the fourth option Probably this one: a=a-b; b=a+b; a=b-a; --~--~-~--~~~---~--~~ You rec

[algogeeks] Re: swap

2006-03-08 Thread [EMAIL PROTECTED]
I could find only 3 1) a= a+b;b=a-b;a=a-b; 2) a= a^b;b=a^b;a=a^b; 3) a= a*b;b=a/b;a=a/b; Did anyone get the fourth option --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this gr