How to convert a number from unsigned to signed?
signed: -32768 to 32767 unsigned: 0 to 65535
I am solving the problem in JavaScript. The situation is that I
In the case that you would be looking for bitwise operation related answers, you could try the following:
(number << 16) >> 16
new Int16Array([number])[0]
Where number in both cases is your 16 bit number.
As a side note the reason the first solution works is because if you bit shift to the right 16 times, the most significant bit of your 16 bit number will actually become the most significant bit of the 32 bit JavaScript integer (so if the most significant bit was a 1, it'd make the number negative), and so when you shift it to the left 16 times it'd shift while keeping the standard 2s complement form and retain the value/sign it gained from being shifted to the right previously, see this Wikipedia article for more:
https://en.m.wikipedia.org/wiki/Arithmetic_shift