3

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# ?

Community
  • 1
  • 1
Mark Segal
  • 5,427
  • 4
  • 31
  • 69
  • 3
    There is no `<<<` operator. There is no need for it as left shift semantics are the same for signed and unsigned numbers. – Joey Jul 25 '11 at 12:35

4 Answers4

7

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#.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

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.

Blam
  • 2,888
  • 3
  • 25
  • 39
2

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.

Bala R
  • 107,317
  • 23
  • 199
  • 210
2

>>> 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.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103