p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C# (http://p2p.wrox.com/forumdisplay.php?f=22)
-   -   Problem to Swap two number in C# (http://p2p.wrox.com/showthread.php?t=43513)

 furqanms June 5th, 2006 01:05 PM

Problem to Swap two number in C#

I have try the following code in Turbo C it works fine.
but when i try the same in C# it not give me a proper result.i dont know why it so

Turbo C
================================================== ===================
#include<stdio.h>
void main()
{
int a,b;
a=10;
b=20;
//Before swap two numbers
printf("a = %d\n",a);
printf("b = %d\n",b);

//After swap two numbers
a^=b^=a^=b;
printf("a = %d\n",a);
printf("b = %d\n",b);
}
Output
a = 10
b = 20
a = 20
b = 10

C#
================================================== ===================
using System;
class myclass
{
public static void Main()
{
int a,b;
a=10;
b=20;
//Before swap two numbers
Console.WriteLine("a = {0}",a);
Console.WriteLine("b = {0}",b);

//After swap two numbers
a^=b^=a^=b;
Console.WriteLine("a = {0}",a);
Console.WriteLine("b = {0}",b);
}

}
Output
a = 10
b = 20
a = 0
b = 10

if any one know then help me to understand why the output is differ in c and c#.

 arielote June 5th, 2006 05:12 PM

Friend:

For me the result is OK, because the expresion a^=b^=a^=b, could be analized like:

a^=b (Call it Z)

a^=b^=a^=b may be read as ((a^=b) ^= (a^=b)), equates Z ^= Z equates 0.

Regards

Ariel

---
Ariel Folonier
San Nicolas
Argentina

 All times are GMT -4. The time now is 10:54 PM.