警告:“整数转换会导致截断” [英] warning : 'integer conversion results in truncation'

查看:752
本文介绍了警告:“整数转换会导致截断”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里得到警告。警告说整数转换会导致截断。即使我删除了typecast(U16),它仍然存在。

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));

提前谢谢!

推荐答案

当您尝试将常量值分配给一个不足以容纳它们的L值时,C编译器不喜欢它。我想编译器作者会假设您自声明常量以来就知道应该使用什么值,因此,如果您有可能截断它的值,那一定是错误的。这是一个可行的解决方案,但可能不是您理想的结果:

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;

这篇关于警告:“整数转换会导致截断”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆