在C#中等效的Java三class制运算符(>>>)?
Java的>>>
运算符的等价物(在C#中)是什么?
(只是为了澄清,我不是指>>
和<<
运算符。)
在C#中,您可以使用无符号整数类型,然后<<
和>>
执行您期望的操作。 有关移位运算符的MSDN文档为您提供了详细信息。
由于Java不支持无符号整数(除了char
),因此这个额外的运算符变得必要。
Java没有无符号左移( <<<
),但不管怎样,你可以从那里转换为uint
和shfit。
例如
(int)((uint)foo >> 2); // temporarily cast to uint, shift, then cast back to int
阅读本文后,我希望我的使用结论如下是正确的。 如果没有,洞察力表示赞赏。
Java的
i >>>= 1;
C#:
i = (int)((uint)i >> 1);
n >>> s in Java相当于TripleShift(n,s),其中:
private static long TripleShift(long n, int s) { if (n >= 0) return n >> s; return (n >> s) + (2 << ~s); }
对于我的VB.Net人员
上面建议的答案将为您提供Option Strict ON
溢出exception
试试这个例如-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
C#中没有>>>运算符。 但是您可以将int,long,Int16,Int32,Int64等值转换为unsigned uint,ulong,UInt16,UInt32,UInt64等。
这是一个例子。
private long getUnsignedRightShift(long value,int s) { return (long)((ulong)value >> s); }