gpt4 book ai didi

c++ - unsigned int - 移位和 oring 产生负值

转载 作者:太空宇宙 更新时间:2023-11-04 00:39:13 24 4
gpt4 key购买 nike

我正在阅读 wav 的标题文件并使用 << 填充我的类(class)成员(左移)和 | (或)当我从文件中读取原始字节时。现在,巧合的是我有以下情况
其中 uBytesPerSecond类型为 unsigneddata属于 char*所以我可以使用 std::fstream::read .

现在,当我关注 uBytesPerSecond 的创建时在调试器中

this->uBytesPerSecond |= data[0x1F]; //0x00 ->uBytesPerSecond = 0x00000000
this->uBytesPerSecond <<= 8; // ->uBytesPerSecond = 0x00000000
this->uBytesPerSecond |= data[0x1E]; //0x02 ->uBytesPerSecond = 0x00000002
this->uBytesPerSecond <<= 8; // ->uBytesPerSecond = 0x00000200
this->uBytesPerSecond |= data[0x1D]; //0xb1 ->uBytesPerSecond = 0xffffffb1 !!!
this->uBytesPerSecond <<= 8; // ->uBytesPerSecond = 0xffffb100
this->uBytesPerSecond |= data[0x1C]; //0x10 ->uBytesPerSecond = 0xffffb110

这种情况下的预期输出是 uBytesPerSecond = 0x00002b110 .请告诉我这是怎么回事,我该如何解决。

我正在使用 MSVC2012和 Windows 8,这是一个 VC++-Console -申请。

最佳答案

问题是对于您的平台,char 是一个有符号类型。因此,包含的值将被符号扩展到 32 位以进行或操作,为 data[0x1D] 提供 0xffffffb1

要解决该问题,只需将data 的类型更改为unsigned char*

编辑:如评论中所述,该问题的另一种解决方案是使用 0xFF 显式屏蔽操作数​​:this->uBytesPerSecond |= data [0x1F] & 0xFF 等等。

关于c++ - unsigned int - 移位和 oring 产生负值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16131390/

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