Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first
The compiler implicitly widened and sign-extended a variable, and then used the resulting value in a bitwise OR operation. This can result in unexpected behavior.
The following sample generates CS0675:
// CS0675.cs
// compile with: /W:3
using System;
public class sign
{
public static void Main()
{
int hi = 1;
int lo = -1;
long value = (((long)hi) << 32) | lo; // CS0675, value contains -1 (0xffffffff_ffffffff)
// try the following line instead
// long value = (((long)hi) << 32) | ((uint)lo); // correct, value contains 8589934591 (0x00000001_ffffffff)
}
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.