Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
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.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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 :)

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!