-32768 not fitting into a 16 bit signed value

前端 未结 4 1006
轻奢々
轻奢々 2021-01-23 15:40

I am using PCLint v 9.00h

In my code I have the following (where S16 is a signed 16 bit):

S16 temperatureResult = -32768;

Which unless

4条回答
  •  甜味超标
    2021-01-23 16:08

    It comes to mind to try:

    S16 temperatureResult = (S16) 0x8000; // ASSUMES twos complement integers

    The explicit cast is because Rule 10.1 says

    "The value of an expression of integer type shall not be implicitly converted to a different underlying type if ..."

    Make it portable:

    S16 temperatureResult = -32767 - 1;

    But anyway, if MISRA requires compatibility with ones complement computers (like some Cray supercomputers), then the guaranteed range of signed 16-bit is only [-32767 ... 32767] so you can't achieve what you're trying to do.

提交回复
热议问题