I am writing a DLL plugin for a 32bit program on Win64, compiled as Win32/Debug in Visual Studio 2015. I need to left bit-shift an unsigned char (defined upstream) by 56 bits into an UINT64, so that 0xFF should become 0xFF000000000000.
What's actually happening is that for any shift value greater than 32, the result is a shift of (shift - 32) bits, with the higher 32 bits of my target UINT64 become all-1, so that:
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));
}
...
}
Using the VS debugger, I see that for:
m->data[i] == 0xC0
i == 0x7
start_byte == 0
bytes == 8 (so that shift == 8 * (7 - 0) == 56)
then:
r == 0xFFFFFFFFc0000000
I have read:
https://msdn.microsoft.com/en-us/library/336xbhcz.aspx
Left shift an integer by 32 bits
The closest to an answer I can see is Andrey Nasonov's answer (https://stackoverflow.com/a/33058550/7817631) to the latter, but I am on a 64-bit machine (Win7-64 on Core i5), so I would not expect this to be directly relevant.