Search code examples
c#bit-shift

What is code for logical right shift in C#?


I am trying to translate Java code with logical right shift (>>>) (Difference between >>> and >>) to C#

Java code is

 return hash >>> 24 ^ hash & 0xFFFFFF;

C# is marked >>> as syntax error.

How to fix that?

Update 1 People recommend to use >> in C#, but it didn't solve problem.

System.out.println("hash 1 !!! = " + (-986417464>>>24));

is 197

but

Console.WriteLine("hash 1 !!! = " + (-986417464 >> 24));

is -59

Thank you!


Solution

  • Java needed to introduce >>> because its only unsigned type is char, whose operations are done in integers.

    C#, on the other hand, has unsigned types, which perform right shift without sign extension:

    uint h = (uint)hash;
    return h >> 24 ^ h & 0xFFFFFF;