在 VS 移位中,UINT64 上的左移(64 >移位> 32)位仅 32 位



我正在为Win64上的32位程序编写一个DLL插件,在Visual Studio 2015中编译为Win32/Debug。我需要将无符号字符(上游定义(位移 56 位到 UINT64 中,以便0xFF变得0xFF000000000000。

实际发生的是,对于任何大于 32 的移位值,结果是(移位 - 32(位的移位,我的目标 UINT64 的较高 32 位变为 all-1,因此:

int     NmeaPgn::sf_get_bytes(TCanMessage *m, UINT8 start_byte, UINT8 bytes)
{
    UINT64 r = 0; /* Could need to fetch up to 8 bytes, as in 60928 */
    INT8 i;
    for (i = start_byte + bytes - 1; i >= start_byte; --i)
    {
        r |= (m->data[i] << 8 * (i - start_byte));
    }
...
}

使用 VS 调试器,我看到:

m->data[i] == 0xC0
i == 0x7
start_byte == 0
bytes == 8 (so that shift == 8 * (7 - 0) == 56)
then:
r == 0xFFFFFFFFc0000000

我读过:

https://msdn.microsoft.com/en-us/library/336xbhcz.aspx

按位左移 32 C++

整数左移 32 位

我能看到的最接近答案的是安德烈·纳索诺夫(Andrey Nasonov(对后者的回答(https://stackoverflow.com/a/33058550/7817631(,但我使用的是64位机器(Core i5上的Win7-64(,所以我不希望这直接相关。

(由于TomGreen还不能回答他自己的问题,这里是他的答案(

我必须显式地将我的无符号字符转换为 UINT64,如下所示:

r |= ((UINT64)m->data[i] << 8 * (i - start_byte));

这将产生期望值(0xC000000000) m->data[i] == 0xC0shift == 56

最新更新