首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# -用于符号扩展操作数的位或运算符;请考虑首先转换为较小的无符号类型

C# -用于符号扩展操作数的位或运算符;请考虑首先转换为较小的无符号类型
EN

Stack Overflow用户
提问于 2011-09-07 09:48:41
回答 3查看 10.4K关注 0票数 21

我知道这些警告可能毫无意义..但不管怎样,我可以摆脱他们?

我收到了7个这样的警告。

Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first

这与OR运算符|有关

我强调了发出警告的原因。

代码语言:javascript
复制
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);
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-09-07 13:22:48

在网上快速搜索一下,就会显示the official documentation for the warning,并附有解释:

编译器隐式地对变量进行加宽和符号扩展,然后在按位OR操作中使用结果值。这可能会导致意外的行为。

问题是表达式v75 | 0x862D63D3的形式是int | uint。这是通过将两端都提升到long来计算的。如果你真的想要签名扩展,那就写(ulong)(long)v75 | 0x862D63D3。如果你真的想要零扩展,那就写(uint)v75 |0x862D63D3

代码语言:javascript
复制
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);
 }
}

此程序打印

代码语言:javascript
复制
ffffffff862d63d3
ffffffff862d63d3
862d63d3

正如您所看到的,编译器默认使用第一种解释,这可能不是您想要的。

票数 29
EN

Stack Overflow用户

发布于 2011-09-07 10:31:03

尝试将v75和其他具有无符号十六进制值的ORed变量强制转换为uint:

代码语言:javascript
复制
((uint)v75 | 0x862D63D3)

或者将变量声明为uint而不是int

票数 3
EN

Stack Overflow用户

发布于 2016-04-20 18:01:46

如果对int和long变量执行OR操作,则系统会将int转换为long。它有两种存在方式:

代码语言:javascript
复制
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)}");
}
}

结果:

代码语言:javascript
复制
int.MinValue  = 10000000000000000000000000000000
long.MinValue = 1000000000000000000000000000000000000000000000000000000000000000

default cast = 1111111111111111111111111111111110000000000000000000000000000000
custom  cast = 0000000000000000000000000000000010000000000000000000000000000000

default long OR int = 1111111111111111111111111111111110000000000000000000000000000000
custom  long OR int = 1000000000000000000000000000000010000000000000000000000000000000

你想要怎样的结果?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7328052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档