符号扩展操作数上使用的C#按位or运算符;首先考虑将一个较小的无符号类型转换为
我知道这些警告可能毫无意义。。但无论如何我可以摆脱他们 我收到了7条警告符号扩展操作数上使用的C#按位or运算符;首先考虑将一个较小的无符号类型转换为,c#,bit-manipulation,compiler-warnings,suppress-warnings,C#,Bit Manipulation,Compiler Warnings,Suppress Warnings,我知道这些警告可能毫无意义。。但无论如何我可以摆脱他们 我收到了7条警告 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 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);
符号扩展操作数上使用的位or运算符;考虑将一个较小的无符号类型转换为
这与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);
尝试将v75和其他使用无符号十六进制值进行或运算的变量强制转换为uint:
((uint)v75 | 0x862D63D3)
或者将变量声明为uint
而不是int
一个快速的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);
}
}
这个程序打印
ffffffff862d63d3
ffffffff862d63d3
862d63d3
如您所见,编译器默认为第一个解释,这可能不是您想要的。如果您对int和long变量执行OR操作,则系统将int强制转换为long。 存在两种方式:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine($"int.MinValue = {Convert.ToString(int.MinValue, 2)}");
Console.WriteLine($"long.MinValue = {Convert.ToString(long.MinValue, 2)}");
Console.WriteLine();
long cast1 = int.MinValue; // !!!
long cast2 = unchecked((uint)int.MinValue); // !!!
Console.WriteLine($"default cast = {Convert.ToString(cast1, 2)}");
Console.WriteLine($"custom cast = {Convert.ToString(cast2, 2)}");
Console.WriteLine();
Console.WriteLine($"default long OR int = {Convert.ToString(long.MinValue | int.MinValue, 2)}");
Console.WriteLine($"custom long OR int = {Convert.ToString(long.MinValue | unchecked((uint)int.MinValue), 2)}");
}
}
结果:
int.MinValue = 10000000000000000000000000000000
long.MinValue = 1000000000000000000000000000000000000000000000000000000000000000
default cast = 1111111111111111111111111111111110000000000000000000000000000000
custom cast = 0000000000000000000000000000000010000000000000000000000000000000
default long OR int = 1111111111111111111111111111111110000000000000000000000000000000
custom long OR int = 1000000000000000000000000000000010000000000000000000000000000000
您希望得到什么样的结果?这可能会因为混合使用有符号和无符号的short和int而变得更加混乱——只需考虑您的子表达式将升级为哪种类型,并遵循公认答案中的建议即可。读者练习:尝试获取“uint x=(我在
long | int
中看到了这个错误……我找不到有符号/无符号的不匹配……当我将int
显式转换为long
时,警告消失了,但是IDE还通知我显式转换是“冗余的”“因为从int
到long
的转换应该是隐式的。我想它无法下定决心。”