Question

Possible Duplicate:
Equivalent of Java triple shift operators (>>> and <<<) in C#?

Java has operator >>> and <<< which are a bit different then >> and << - can anyone give me its equivalent in C# ?

Was it helpful?

Solution

The simplest (or at least most logical) equivalent is effectively an unchecked cast to the equivalent unsigned type, followed by a normal shift and then potentially a cast back again:

// To perform int result = x >>> 5;
int x = -10;

uint u = unchecked ((uint) x);
u = u >> 5;

int result = unchecked ((int) u);

(The unchecked part is only relevant if you're otherwise in a checked context, of course.)

In my experience, times where you normally want to use >>> in Java, you'd just use unsigned types to start with in C#.

OTHER TIPS

There is no c# equivalent, if you use an unsigned value on the left, >> in c# will perform the same function as >>> in java.

You therefore need to cast to get the desired effect.

Java has >>> (I don' think there is a <<< operator) which is the unsigned right shift operator that is not present in c#. It is there in java as java has not unsigned data types. In c# just use an unsigned type with >> operator.

>>> is an unsigned shift operations in Java.

They don't have an equivalent in C# because C# supports unsigned integers and hence you can just shift on those.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top