warning : 'integer conversion results in trunc

2019-07-09 20:15发布

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条回答
男人必须洒脱
2楼-- · 2019-07-09 20:30

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;
查看更多
登录 后发表回答