Java 运算符的等价物(在 C# 中)是>>>
什么?
(澄清一下,我指的不是>>
and<<
运算符。)
在 C# 中,您可以使用无符号整数类型,然后使用<<
and>>
执行您期望的操作。MSDN 文档中有关班次操作员的信息为您提供了详细信息。
由于 Java 不支持无符号整数(除了char
),因此这个额外的运算符变得很有必要。
Java 没有无符号左移 ( <<<
),但无论哪种方式,您都可以uint
从那里强制转换和 shfit。
例如
(int)((uint)foo >> 2); // temporarily cast to uint, shift, then cast back to int
看完本文,我希望我的以下使用结论是正确的。如果没有,见解赞赏。
爪哇
i >>>= 1;
C#:
i = (int)((uint)i >> 1);
Java 中的 n >>> s 等价于 TripleShift(n,s) 其中:
private static long TripleShift(long n, int s)
{
if (n >= 0)
return n >> s;
return (n >> s) + (2 << ~s);
}
C# 中没有 >>> 运算符。但是您可以将 int、long、Int16、Int32、Int64 等值转换为 unsigned uint、ulong、UInt16、UInt32、UInt64 等。
这是示例。
private long getUnsignedRightShift(long value,int s)
{
return (long)((ulong)value >> s);
}
对于我的VB.Net人
上面建议的答案将为您提供溢出异常Option Strict ON
例如-100 >>> 2
,使用上述解决方案试试这个:
以下代码始终适用于>>>
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