Bug 10320 - Wrong results for some expressions with "(ulong)", "-" and ">>" operators
Summary: Wrong results for some expressions with "(ulong)", "-" and ">>" operators
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: All All
: --- normal
Target Milestone: ---
Assignee: Bugzilla
Depends on:
Reported: 2013-02-15 10:55 UTC by Rebex.NET
Modified: 2013-02-19 09:25 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Rebex.NET 2013-02-15 10:55:15 UTC
Some expressions with unary '-' operator, bitwise shifting and conversion to ulong produce wrong results.

Test case #1:
  long a = 0x100000000;
  ulong b = (ulong) (a) >> 32;
  ulong c = (ulong) (-(-a)) >> 32; // this produces wrong result in Mono

Excected output: "1==1" (just like in .NET).
However, the actual results are different in 32bit Mono:

Mono 2.10.9 (tarball Mon May 7 20:25:51 EDT 2012) - x86 - MacOS X 10.8.2:

Mono 2.6.7 (Debian 2.6.7-5.1) - x86 - Debian 6.0:
Mono (Debian - x86 - Debian 7.0:

Interestingly, 64bit version of Mono produces correct result (just like .NET):

Mono 2.10.9 (tarball Mon Nov 19 16:16:18 CET 2012) - amd64 - MacOS X 10.8.2:

MS .NET 4.5 (64bit version) - Windows 8:

MS .NET 4.5 (32bit version) - Windows 8:


Test case #2:
  long a = -1;
  ulong b = ((ulong)1) >> 32;
  ulong c = ((ulong)-a) >> 32; // this produces wrong result in Mono
  Console.WriteLine("{0}=={1}", b, c);

Expected output:
  0 == 0

Result on Mono (Debian - x86 - Debian 7.0:
  1 == 0
(have not tried the other platform)
Comment 1 Rebex.NET 2013-02-15 14:59:29 UTC
Sorry, there is an error in the test case #1 code - please use Console.WriteLine("{0}=={1}", b, c) instead of Console.WriteLine("{0}=={1}")
Comment 2 Zoltan Varga 2013-02-19 09:25:30 UTC
Fixed in master/2.10 branches. Thanks for the report.