@ tommyvdp - What you need to do is called sign extension. Basically when you promote a signed number from a lower bit count to a higher bit count you need to extend the sign bit to fill the higher bit values. So in your case the you want to promote a 12 bit number to a 16 bit number so you need to extend the sign bit to fill the 4 additional bits.
Your 12 bit number 3087 is represented in binary as
To put this into a short and have it be treated correctly you need extend the sign bit which would give the following bit pattern
These bits match the sign bit, if the sign bit had been a 0 ie. a positive number then these bits would have been all 0s.
So how do you sign extend the number, well here is some quick bit math (untested!!!)
short SignExtend(short value, signBit)
int signMask = 1 << signBit;
if ((value & singMask) != 0)
return (short)((short)(0xffff & ~(signMask - 1)) | value);
I hope that is right, but basically, we are checking if the sign bit is a 1 then we create an mask which will make all the high bits above the sign bit 1s and then we or the original value with the mask to get the sign extended value.
This code is a little naive, but I can’t think of a better way now. When I am at my dev PC I will see if my brain is working a little better. But hopefully this gets you going.