按位或在Visual Studio 2015年一个符号扩展操作使用的运营商 [英] Bitwise-or operator used on a sign-extended operand in Visual Studio 2015

查看:281
本文介绍了按位或在Visual Studio 2015年一个符号扩展操作使用的运营商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是试图安装Visual Studio 2015年,并试图编译一个老项目时,我得到了警告

I just tried installing Visual Studio 2015, and when trying to compile an old project, I got the warning

CS0675按位或上一个符号扩展操作使用的运营商;考虑
  铸造到一个较小的无符号类型第一个

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

换了一块code的在Visual Studio 2013年编译时,不给同样的警告我发现所有需要重现这是很简单的code:

for a piece of code that does not give the same warning when compiling in Visual Studio 2013. I found out that all it takes to reproduce is this very simple code:

short a = 0;
int b = 0;

a |= (short)b;

现在,我已阅读<一个href=\"http://stackoverflow.com/questions/7328052/c-sharp-bitwise-or-operator-used-on-a-sign-extended-operand-consider-casting-to\">this SO质疑,我读<一个href=\"http://blogs.msdn.com/b/ericlippert/archive/2010/11/29/bit-twiddling-what-does-warning-cs0675-mean.aspx\">Eric利珀特的博客文章在这个问题上,我很快就阅读了关于符号扩展,但我的理解是,迹象当你从由较少的比特数的符号数类型转换成带有位,如更多数量的扩展会发生短 INT 例如

Now, I have read this SO question, I have read Eric Lippert's blog post on this issue, and I quickly read up on sign extension, but my understanding is that sign extension happens when you cast from a signed number type consisting of a smaller number of bits to one with a larger number of bits, such as short to int for example.

不过,因为我从 INT 强制转换为,如果我没有符号扩展应该发生。我们把。这不发出在早期版本的Visual Studio的警告的事实,这使我相信这一定是在Visual Studio 2015年编译器(罗斯林)的错误。我误解了如何扩展签字和/或编译器在这里工作,或者这是最有可能是编译器缺陷?

But since I'm casting from an int to a short, no sign extension should happen if I'm not mistaken. The fact that this does not issue a warning in earlier versions of Visual Studio, it leads me to believe that this must be a bug in the Visual Studio 2015 compiler (Roslyn). Am I misunderstanding how sign extension and/or the compiler works here, or is this most likely a compiler bug?

乔恩斯基特指出,实际上确实是一个符号扩展发生,因为 | 运营商还没有为等有一个隐式转换为 INT 结果之前转换回短期再次。然而,编译器不应该因为演员是无害发出这样的警告。有一个在罗斯林编译器中的错误作为接受的答案指出。

Jon Skeet pointed out that there actually is indeed a sign extension happening since the | operator isn't defined for short and so there's an implicit cast to int before the result is cast back to short again. However, the compiler shouldn't have issued this warning since the cast is harmless. There was a bug in the Roslyn compiler as pointed out in the accepted answer.

推荐答案

这仅仅是一个错误。是在VS2015开发很晚加入code检测并报告该错误(请参见 https://github.com / DOTNET /罗斯林/问题/ 909 https://github.com/dotnet/roslyn/pull/2416 ),并将其检测相比VS2013老泪。现在有一个bug报告(<一个href=\"https://github.com/dotnet/roslyn/issues/4027\">https://github.com/dotnet/roslyn/issues/4027)修复这一点。

This is just a bug. The code to detect and report this error was added very late in the VS2015 development (see https://github.com/dotnet/roslyn/issues/909 and https://github.com/dotnet/roslyn/pull/2416) and it detects too many cases compared to VS2013. There is now a bug report (https://github.com/dotnet/roslyn/issues/4027) to repair this.

这篇关于按位或在Visual Studio 2015年一个符号扩展操作使用的运营商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆