Re: [algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-13 Thread Felipe Ferreri Tonello

On 06/13/2011 05:22 PM, tech rascal wrote:

just a small doubt.
if x=10 and y=20
thn  wht wud b the value of (x ^ y)?
acc to meit has to be 1 bt if the value is 1 , thn how wud the 
method using XOR operations work?.plz xplain



On Mon, Jun 13, 2011 at 3:18 AM, KK > wrote:


@kunal
Actually its not same value its the same variable and it arises only
if u code the given way(and some people do it this way)

void swap(int &a, int &b)
{
a ^= b ^= a ^= b;
}

now we have
int a = 10;

swap(a, a)

This will set a's value to 0...and this happens while sorting arrays
when u pass the same index values...
Conclusion:  Either code as given in above Q or use the temporary
var...

--
You received this message because you are subscribed to the Google
Groups "Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com
.
To unsubscribe from this group, send email to
algogeeks+unsubscr...@googlegroups.com
.
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.


--
You received this message because you are subscribed to the Google 
Groups "Algorithm Geeks" group.

To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.


Try this

void swap(int *x, int *y)
{
if (*x != *y) {
*x ^= *y;
*y ^= *x;
*x ^= *y;
}
}

--
Felipe Ferreri Tonello
felipetonello.com

--
You received this message because you are subscribed to the Google Groups "Algorithm 
Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-13 Thread tech rascal
just a small doubt.
if x=10 and y=20
thn  wht wud b the value of (x ^ y)?
acc to meit has to be 1 bt if the value is 1 , thn how wud the method
using XOR operations work?.plz xplain


On Mon, Jun 13, 2011 at 3:18 AM, KK  wrote:

> @kunal
> Actually its not same value its the same variable and it arises only
> if u code the given way(and some people do it this way)
>
> void swap(int &a, int &b)
> {
> a ^= b ^= a ^= b;
> }
>
> now we have
> int a = 10;
>
> swap(a, a)
>
> This will set a's value to 0...and this happens while sorting arrays
> when u pass the same index values...
> Conclusion:  Either code as given in above Q or use the temporary
> var...
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-12 Thread KK
@kunal
Actually its not same value its the same variable and it arises only
if u code the given way(and some people do it this way)

void swap(int &a, int &b)
{
 a ^= b ^= a ^= b;
}

now we have
int a = 10;

swap(a, a)

This will set a's value to 0...and this happens while sorting arrays
when u pass the same index values...
Conclusion:  Either code as given in above Q or use the temporary
var...

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-12 Thread Anika Jain
let x,y be them..
x=x-y;
y=x+y;
x=y-x;

On Sun, Jun 12, 2011 at 9:28 AM, nicks  wrote:

> Hers's another one line solution for swapping two variables a and b
>
> a=b+a-(b=a)
>
>
> On Sat, Jun 11, 2011 at 2:55 PM, Andreas Hahn 
> wrote:
>
>> Well, on pod, this is an alternative to swapping two variables with a
>> third temporary, but be careful with objects!
>> It won't work on all objects and in C++0x there is the "move" for this
>> purpose.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-11 Thread nicks
Hers's another one line solution for swapping two variables a and b

a=b+a-(b=a)

On Sat, Jun 11, 2011 at 2:55 PM, Andreas Hahn wrote:

> Well, on pod, this is an alternative to swapping two variables with a
> third temporary, but be careful with objects!
> It won't work on all objects and in C++0x there is the "move" for this
> purpose.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-11 Thread Andreas Hahn
Well, on pod, this is an alternative to swapping two variables with a
third temporary, but be careful with objects!
It won't work on all objects and in C++0x there is the "move" for this
purpose.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-11 Thread rohit
can u pls explain the overflow and underflow in +- approach

On Jun 11, 8:58 am, Wladimir Tavares  wrote:
> Swapping two variables without using a temporary variable using the + and -:
>
> x = a;
> y = b;
>
> x = x + y / / x = a + b;
> y = x - y / / y = a + b-b = a;
> x = x - y / / x = a + b-a = b;
>
> y = b;
> x = a;
>
> Problems with this approach:
> 1) It can cause overflow in the operation (+)
> 2) It can cause underflow on operation (-)
>
> Swapping two variables without using variables
> Temporary using XOR:
>
> x = a;
> y = b;
>
> x = x ^ y;
> y = x ^ y / / y = (x xor y) xor y = x xor (y xor y) xor x = 0 = x
> x = x ^ y / / x = (x xor y) xor x = (x xor y) xor y xor x = (x xor x) = y
> xor y = 0
>
> Note that we use some properties of XOR:
>
> 1) Associativity
> 2) Commutativity
> 3) X = X 0 XOR
>
> We have no problems neither underflow nor overflow!
>
> Wladimir Araujo Tavares
> *Federal University of Ceará
>
> *

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-11 Thread Kunal Patil
@KK: Why would XOR method fail when we pass same values of a & b ??

x = 6
y = 6

x = x ^ y; // x  ==>  0
y = x ^ y //  y  ==>  0 ^ 6  ==> 6
x = x ^ y //  x  ==> 6 ^ 0 ==> 6

So after XORing values of X and Y have been swapped as it is the case
normally.

On Sat, Jun 11, 2011 at 11:39 PM, KK  wrote:

> It wont give correct answer when u pass same values of a and b and
> such conditions arises in sorting
>
>
> --
> ***
> Kunal Kapadia
> Computer Science and Engineering
> B.Tech 2nd yr
> NIT Trichy
> ***
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



[algogeeks] Re: Swapping two variables without using a temporary variable

2011-06-11 Thread KK
It wont give correct answer when u pass same values of a and b and
such conditions arises in sorting


--
***
Kunal Kapadia
Computer Science and Engineering
B.Tech 2nd yr
NIT Trichy
***

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.