Convert Raw 14 bit Two's Complement to Signed 16 bit Integer

后端 未结 5 945
一向
一向 2021-01-17 14:08

I am doing some work in embedded C with an accelerometer that returns data as a 14 bit 2\'s complement number. I am storing this result directly into a uint16_t

5条回答
  •  半阙折子戏
    2021-01-17 14:47

    To convert the 14-bit two's-complement into a signed value, you can flip the sign bit and subtract the offset:

    int16_t raw_signed = (raw ^ 1 << 13) - (1 << 13);
    

提交回复
热议问题