Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
402 views
in Technique[技术] by (71.8m points)

c# - Bitwise-or operator used on a sign-extended operand in Visual Studio 2015

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

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

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;

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.

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?

Update

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Sign extension is happening, but possibly not for the obvious reason, and not in a worrying way, IMO.

This code:

a |= (short) b;

is equivalent to:

// No warning here... (surprisingly, given that `a` is being sign-extended...)
a = (short) (a | (short) b);

Which is equivalent to:

// No warning here...
a = (short) ((int) a | (int) (short) b;

because the | operator isn't defined for short operands anyway. Both operands are promoted to int, then the result is cast back to short.

It's not clear why the compiler decides to warn in this case, but there is sign extension occurring... albeit in a harmless way.

Note that you get the same warning if no int variables are involved at all:

short a = 10;
short b = 20;
a |= b; // CS0675

Given the way that the | operator works with casts, this looks entirely harmless to me. I'm not sure whether I'd call it a compiler bug, but it definitely looks like non-ideal behaviour to me. Will ping C# compiler team members to see what I've missed :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...