Unsigned right shift in C # Using Java semantics for negative numbers

I am trying to port Java code to C # and I am running the odd errors associated with the unsigned shift right operator ->> usually the code:

long l = (long) ((ulong) number) >> 2; 

There would be a Java equivalent:

 long l = number >>> 2; 

However, for case -2147483648L , which you could recognize as Integer.MIN_VALUE , this returns a different number than it would be in Java, since casting to ulong changes the semantics of the number, so I get a different result.

How is this possible in C #?

I would like to preserve the semantics of the code as much as possible, since its code is rather complicated.

+6
source share
1 answer

I believe your expression is incorrect when considering order priority in C #. Your code, I believe, converts your long to ulong , and then back to long , and then switches. I assume that you intend to shift to ulong .

From C # Specification Β§7.2.1 , Unary (or, in your case, the casting operation) takes precedence over the shift. So your code is:

 long l = (long) ((ulong) number) >> 2; 

will be interpreted as:

 ulong ulongNumber = (ulong)number; long longNumber = (long)ulongNumber; long shiftedlongNumber = longNumber >> 2; 

Given a number as -2147483648L , this gives 536870912 .

Conversion wrap and offset in brackets:

 long l = (long) (((ulong) number) >> 2); 

It produces logic that can be rewritten as:

 ulong ulongNumber = (ulong)number; ulong shiftedulongNumber = ulongNumber >> 2; long longShiftedNumber = (long)shiftedulongNumber; 

Which value of number like -2147483648L , it gives 4611686017890516992 .


EDIT: Please note that given these ordering rules, my answer has an extra set of brackets that are not needed. The correct expression can be written as:

 long l = (long) ((ulong) number >> 2); 
+5
source

All Articles