liweiliv
liweiliv

Reputation: 41

bitwise operation left shift in msvc is diffrent from linux gcc?

On Linux using GCC, 1UL<<44 is 0x100000000000

But on Windows using MSVC, 1UL<<44 is 0

I am so confused.

Does this show that on Windows a left shift can not shift more than 32 bits?

Upvotes: 0

Views: 458

Answers (1)

In Windows unsigned longs are 32 bits wide. This is due to the unfortunate APIs always expecting unsigned longs to be synonymous with 32 bits, (4 bytes) whereas in Unixen having unsigned longs were equally incorrectly often assumed to be of suitable size for storing a pointer. Hence in Unixen it made sense to use 64 bits unsigned longs and in Windows there is no choice but to keep them 32 bits.

Of course it would have always be correct to just use uint32_t and uintptr_t respectively for these, but lots of code was written before those were standardized.


Notice that the standard says that the behaviour is undefined if the shift amount is negative,, or greater than or equal to the width of shifted type so on Windows 1UL << 32 has undefined behaviour. This is because there are hardware that zero the target if a wide shift happens, hardware that aborts and hardware that only consider the shifts modulo 32, i.e. in this case executing 1UL << 0 which of course equals 1. So since this operation makes no sense in any portable program some C compilers can happily assume that they never happen.

Upvotes: 2

Related Questions