开发者

What does "Lower precision in wider context" warning actually mean?

I have the following code, for an embedded platform where an int is 16 bits and a long int is 32 bits:

#define MULTIPLIER 0x1000

static void my_function(uint16_t i, void *p)
{
    uint32_t start = MULTIPLIER * i;
    ...
}

My compiler gives me the 开发者_C百科warning:

Warning 1 : lower precision in wider context: '*'

for this line.

What does this really mean? I can make the warning go away by changing the #define to

#define MULTIPLER 0x1000ul

(explicitly making it an unsigned long) but I would like to understand the warning.


It's warning you that the multiplication will take place using 16-bit values, and the 16-bit result will then be converted to a 32-bit result. This might not be what you expect (the 16-bit multiplication might overflow), hence the warning.

This forum posting covers the issue


I think the warning means that you probably want a 32-bit result, but since 0x1000 has type int and i has type int16_t, the expression is only type int and likely to overflow and not give you the results you want.


For the expression MULTIPLIER * i the compiler chooses a type of greater precision from types of operands. In the first case you give it two ints and that means you will have a truncated result, that will afterwards be converted to long int, but the precision will be still 16 bits.

in the latter case you explicitly make one of your operands long, and the result is then written to a variable of the same precision.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜