Learning the hard way, I tried to left shift a long long
and uint64_t
to more than 32 bits on an x86 machine resulted 0
. I vaguely remember to have read somewhere than on a 32 bit machine shift operators only work on the first 32 bits but cannot recollect the source.
I would like to know is if Shifting more than 32 bits of a uint64_t integer on an x86 machine is an Undefined Behavior?
The standard says (6.5.7 in n1570):
3 The integer promotions are performed on each of the operands. The type of the result is that of the promoted left operand. If the value of the right operand is negative or is greater than or equal to the width of the promoted left operand, the behavior is undefined.
4 The result of E1 << E2 is E1 left-shifted E2 bit positions; vacated bits are filled with zeros. If E1 has an unsigned type, the value of the result is E1 × 2E2 , reduced modulo one more than the maximum value representable in the result type. If E1 has a signed type and nonnegative value, and E1 × 2E2 is representable in the result type, then that is the resulting value; otherwise, the behavior is undefined.
5 The result of E1 >> E2 is E1 right-shifted E2 bit positions. If E1 has an unsigned type or if E1 has a signed type and a nonnegative value, the value of the result is the integral part of the quotient of E1 / 2E2 . If E1 has a signed type and a negative value, the resulting value is implementation-defined.
Shifting a uint64_t
a distance of less than 64 bits is completely defined by the standard.
Since long long
must be at least 64 bits, shifting long long
values less than 64 bits is defined by the standard for nonnegative values, if the result doesn't overflow.
Note, however, that if you write a literal that fits into 32 bits, e.g. uint64_t s = 1 << 32
as surmised by @drhirsch, you don't actually shift a 64-bit value but a 32-bit one. That is undefined behaviour.
The most common results are a shift by shift_distance % 32
or 0, depending on what the hardware does (and assuming the compiler's compile-time evaluation emulates the hardware semantics, instead of nasal demons.)
Use 1ULL < 63
to make the shift operand unsigned long long
before the shift.