gpt4 book ai didi

c++ - 编译器警告使 int8_t 的复合赋值提升为 int 感到困惑

转载 作者:IT老高 更新时间:2023-10-28 23:14:19 24 4
gpt4 key购买 nike

我通常可以理解编译器警告背后的原因,但这似乎完全是错误的。

#include <stdint.h>    
uint8_t myfunc(uint8_t x,uint8_t y)
{
x |= y;
return x;
}

带有 -Wall 的 intel 编译器报错:

conversion from "int" to "uint8_t={unsigned char}" may lose significant bits
x |= y;
^

这是对的吗?上面的代码是不是不可移植和非标准的?

最佳答案

这是 integer Promotions 在起作用。

x |= y;

| 运算符的两个操作数都被提升为 int

x = (int)x | (int)y;

然后将结果转换回 uint8_t 失去精度。

关于c++ - 编译器警告使 int8_t 的复合赋值提升为 int 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5871298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com