[Mono-bugs] [Bug 29000][Nor] Changed - Incorrect right-evaluation in assignament

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Thu, 17 Apr 2003 16:05:03 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by miguel@ximian.com.

http://bugzilla.ximian.com/show_bug.cgi?id=29000

--- shadow/29000	Fri Apr 11 15:18:07 2003
+++ shadow/29000.tmp.32078	Thu Apr 17 16:05:03 2003
@@ -1,13 +1,13 @@
 Bug#: 29000
 Product: Mono/MCS
 Version: unspecified
 OS: Debian Woody
 OS Details: 
-Status: NEW   
-Resolution: 
+Status: RESOLVED   
+Resolution: FIXED
 Severity: Unknown
 Priority: Normal
 Component: Misc
 AssignedTo: mono-bugs@ximian.com                            
 ReportedBy: sergio.gomez@consejo-eps.uco.es               
 QAContact: mono-bugs@ximian.com
@@ -130,6 +130,50 @@
 should do, I have no idea yet.
 
 
 ------- Additional Comments From martin@gnome.org  2002-09-09 14:45 -------
 This doesn't need to be assigned to me.
 
+
+------- Additional Comments From miguel@ximian.com  2003-04-17 16:05 -------
+Ok, so turns out that this is not a bug in the compiler, but rather, a
+feature of the C# language.  Today on DOTNET-CLR list:
+
+From: Jonathan Keljo <jkeljo@microsoft.com>
+
+Subject: 	RE: Question about using XOR to swap without a temporary
+variable
+Date: 	Thu, 17 Apr 2003 12:59:03 -0700	
+Actually in this case it's a matter of different language definitions.
+
+In C++, the order of evaluation of operands is undefined [Stroustrup 3rd
+Ed., Section 6.2.2), so the compiler can choose to evaluate them however
+it likes. It happens that your C++ compiler seems to do it from right to
+left in this case. In C#, however, the order of evaluation is operands
+is defined to be left-to-right (C# Standard, section 14.2). So, if you
+expand out all the ^='s to be the appropriate combination of ^ and =,
+you get:
+
+a = a ^ (b = b ^ (a = a ^ b))
+
+Which, based on the C# rules would evaluate all of the operands before
+any of the XORs take place, leading to the results you see. Working it
+out step-by-step looks like this:
+
+a = a ^ (b = b ^ (a = a ^ b))
+a = 4 ^ (b = b ^ (a = a ^ b))
+a = 4 ^ (b = 5 ^ (a = a ^ b))
+a = 4 ^ (b = 5 ^ (a = 4 ^ b))
+a = 4 ^ (b = 5 ^ (a = 4 ^ 5))
+a = 4 ^ (b = 5 ^ (a = 1)) [a set to 1 at this point)
+a = 4 ^ (b = 5 ^ 1)
+a = 4 ^ (b = 4)                     [b set to 4 at this point]
+a = 4 ^ 4
+a = 0                                 [a set to 0 here]
+
+Jonathan
+
+--------------------------------------------------------
+
+Am hence, closing the bug.
+
+La la la.