I have a problem with masking an Int16, C# interprets the mask's high hex number (65532d) as an Int32. I have seen many examples but they have either UInt or use friendly sunshine low-bit masks that don't interfere with the sign bit so no conversion/casting is required. How do I make the two numbers get along? Look at the FFFC below:
Error code is: Error CS0266 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)
for (i = 0; i < nbrofsamples; i++) {
// bigbuffer[] contains a list of interlaced Int16 from up to 4 data channels
x = (i* SRecMeasPtrC.uNumCurves + SRecMeasPtrC.uCurveIndex) * sizeof(Int16);
intvalue = BitConverter.ToInt16(bigbuffer, x);
// intvalue's two lower bits are used for other stuff so I want to mask them away
intvalue = intvalue & 0xFFFC; // << ERROR! Meh! Hex masking works in C and in Delphi!
// and measvalue (a float) is calculated by applying a magic formula:
measvalue = Myvalue3(intvalue, CurrentKanalInfos[CurrentChannel], out iserr);
// and then I copy the float into an array (belonging to the channel) that can be plotted onscreen
Buffer.BlockCopy(BitConverter.GetBytes(measvalue), 0, Mymeasdata[CurrentChannel].curvebufferY, i * sizeof(Single), sizeof(Single));
}
Edit: Now, I have received links to a couple of answers that give "whys" but no "hows". One (part of) an answer is: "An & operator exists for int and there is also an implicit cast from byte to int so when you write byte1 & byte2 this is effectively the same as writing ((int)byte1) & ((int)byte2) and the result of this is an int. " One answer that was up for one hour here gave me the -4 to use instead of 0xFFFC and I am grateful for that workaround. However the more easy to read hex mask "FFFC" will yet have to be in the comments, not in code.
So this is what finally didn't produce an error, all parentheses needed:
Int16 intvalue;
intvalue = (Int16)(((int)intvalue) & ((int)-4)) // -4 is my bitmask FFFC