SSp*_*oke 19 c# bit-manipulation compiler-warnings suppress-warnings
我知道这些警告可能毫无意义..但无论如何我可以摆脱它们?
我收到了7个这样的警告.
Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first
这与OR运算符有关 |
我强调了发出警告的内容.
int result = (int)ror((uint)(v76 ^ (v75 | 0x862D63D3)), (uint)(BitConverter.ToInt32(v4, 72) ^ 0x22));
int v11 = (int)rol((uint)(int)((v8 & v10 | ~v10 & 0xEFCDAAC9) + v3[2] - 1126481991), 17);
int v144 = (int)rol((uint)(int)((v141 & v143 | ~v143 & 0xEFCDAAC9) + v3[2] - 1126481991), 17);
int v77 = (int)(`BitConverter.ToInt32(v4, 52) | 0x96C35837`);
BitConverter.GetBytes((int)(v30 & 0x870DEA8A | v29)).CopyTo(v2, 32);
int temp24 |= (int)(BitConverter.ToInt32(v3, 48) | 0x96B4A1B4);
int v17 = (int)(BitConverter.ToInt32(v3, 12) | 0x83868A1D);
Run Code Online (Sandbox Code Playgroud)
Ray*_*hen 26
快速Web搜索显示警告的官方文档,其中包含一个解释:
编译器隐式地扩展并对变量进行符号扩展,然后在按位OR运算中使用结果值.这可能会导致意外行为.
问题是表达式v75 | 0x862D63D3的形式int | uint.这是通过促进双方来计算的long.如果你真的想要签名扩展,请写(ulong)(long)v75 | 0x862D63D3.如果你真的想要零扩展,那就写吧(uint)v75 |0x862D63D3.
class Program {
public static void Main()
{
int v75 = int.MinValue;
System.Console.WriteLine("{0:x}", v75 | 0x862D63D3);
System.Console.WriteLine("{0:x}", (ulong)(long)v75 | 0x862D63D3);
System.Console.WriteLine("{0:x}", (uint)v75 | 0x862D63D3);
}
}
Run Code Online (Sandbox Code Playgroud)
这个程序打印
ffffffff862d63d3
ffffffff862d63d3
862d63d3
Run Code Online (Sandbox Code Playgroud)
如您所见,编译器默认为第一种解释,这可能不是您想要的.