为什么这个分配会产生一个comile错误:Constant value '-2147483648' cannot be converted to a 'ulong'
我必须在unchecked (...)
这种情况下使用?
ulong xDummy30 = (1 << 30); // works
ulong xDummy31 = (1 << 31); // ERROR 25 Constant value '-2147483648' cannot be converted to a 'ulong'
ulong xDummy32 = (1 << 32); // works
使用这个代替工作:
ulong xDummy31a = unchecked((ulong)(1 << 31));
// or
ulong xDummy31b = (1ul << 31); // comment from Regis Portalez
编辑
问题为什么在 C# 中执行按位运算时必须强制转换 0(零)?有类似的答案,观察到的行为的原因是相同的。但它们是不同的问题。