Signed to Unsigned and Vice Versa (arithmetical)

后端 未结 3 876
逝去的感伤
逝去的感伤 2021-01-16 13:43

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

3条回答
  •  清歌不尽
    2021-01-16 13:51

    function signed(bits, value) { return value & (1 << (bits - 1)) ? value - (1 << bits) : value; }

    signed(8, 0xFF); // returns -1

    signed(16, 0xFF); // returns 255

提交回复
热议问题