Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is code for logical right shift in C#?

Tags:

c#

bit-shift

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!

like image 957
Arthur Avatar asked Dec 03 '25 12:12

Arthur


1 Answers

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;
like image 183
Sergey Kalinichenko Avatar answered Dec 05 '25 01:12

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!