I am using the following code to sign extend a 12 bit value I unpacked from 1.5 bytes to 16 bits:
word[0] |= ((word[0] & 0x800) != 0 ? (Int16)(-4096) : (Int16)0);
If I don't cast the last zero to Int16 I get the following complaints from the compiler:
Warning 1 Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first
Error 2 Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?)
Why is this? I understand that C# converts everything to an int when doing bitwise operations, but normally integer constants are automatically given the right type. If I assign zero to a float I don't have to cast it to float first, for example. I'm a C programmer, so please keep that in mind when answering :-)