38

What is the equivalent (in C#) of Java's >>> operator?

(Just to clarify, I'm not referring to the >> and << operators.)

Nayuki
  • 17,911
  • 6
  • 53
  • 80
Nikolaos
  • 1,449
  • 2
  • 15
  • 19
  • 10
    There is no <<< operator in Java, only a >>> operator. – Jesper Dec 10 '09 at 12:30
  • 1
    C# 11 supports >>> operator. check [My answer](https://stackoverflow.com/questions/1880172/equivalent-of-java-triple-shift-operator-in-c/73533057#73533057) and here is [Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#unsigned-right-shift-operator-) – Arjun Vachhani Aug 30 '22 at 06:59

7 Answers7

33

Edit: The Unsigned right-shift operator >>> is now also available in C# 11 and later.

For earlier C# versions, you can use unsigned integer types, and then the << and >> do what you expect. The MSDN documentation on shift operators gives you the details.

Since Java doesn't support unsigned integers (apart from char), this additional operator became necessary.

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • C# 11 supports >>> operator. check [My answer](https://stackoverflow.com/questions/1880172/equivalent-of-java-triple-shift-operator-in-c/73533057#73533057) and here is [Microsoft Docs](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#unsigned-right-shift-operator-) – Arjun Vachhani Aug 30 '22 at 07:01
17

Java doesn't have an unsigned left shift (<<<), but either way, you can just cast to uint and shfit from there.

E.g.

(int)((uint)foo >> 2); // temporarily cast to uint, shift, then cast back to int
Matt
  • 43,482
  • 6
  • 101
  • 102
2

Upon reading this, I hope my conclusion of use as follows is correct. If not, insights appreciated.

Java

i >>>= 1;

C#:

i = (int)((uint)i >> 1);
KeithC
  • 447
  • 4
  • 3
2

n >>> s in Java is equivalent to TripleShift(n,s) where:

    private static long TripleShift(long n, int s)
    {
        if (n >= 0)
            return n >> s;
        return (n >> s) + (2 << ~s);
    }
1

There is no >>> operator in C#. But you can convert your value like int,long,Int16,Int32,Int64 to unsigned uint, ulong, UInt16,UInt32,UInt64 etc.

Here is the example.

    private long getUnsignedRightShift(long value,int s)
    {
        return (long)((ulong)value >> s);
    }
Musakkhir Sayyed
  • 7,012
  • 13
  • 42
  • 65
1

C# 11 and later supports >>> Unsigned right shift operator

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators#unsigned-right-shift-operator-

Arjun Vachhani
  • 1,761
  • 3
  • 21
  • 44
0

For my VB.Net folks

The suggested answers above will give you overflow exceptions with Option Strict ON

Try this for example -100 >>> 2 with above solutions:

The following code works always for >>>

Function RShift3(ByVal a As Long, ByVal n As Integer) As Long
        If a >= 0 Then
            Return a >> n
        Else
            Return (a >> n) + (2 << (Not n))
        End If
End Function
Charles Okwuagwu
  • 10,538
  • 16
  • 87
  • 157