warning : 'integer conversion results in truncation'

自作多情 提交于 2019-12-24 02:05:40

问题


I get an warning here. The warning says 'integer conversion results in truncation'. It persists even if I remove the typecast(U16).

typedef unsigned short  U16;
U16 mask;
mask = ~(U16)(0x8000);

How do I resolve this warning? I used the below code and removed the warning, but unsure if its the right way to do it.

mask = (U16)(~(U32)(0x8000));

Thanks in advance!


回答1:


C compilers don't like when you try to assign constant values into an L-value that's not big enough to hold them. I would guess that the compiler authors assume you know what value should be used since you're declaring a constant, therefore something must be wrong if you're potentially truncating its value. Here's a solution that will work, but may not be your ideal outcome:

typedef unsigned short  U16;
U16 mask;
mask = 0x7fff; //~0x8000;


来源:https://stackoverflow.com/questions/9479403/warning-integer-conversion-results-in-truncation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!