3.5.1 Low-Level Code, Bit-Level Operations

When migrating from a 32-bit application to a 64-bit application, bit-shifting operations can lead to errors. Untyped integral constants are assumed to be of type unsigned int. During shifting, this assumption can lead to unexpected truncation. For example, the maximum possible value of i in the following code sample is 31 because the implicit type of 1 is unsigned int.

long j = 1 << i;

To allow the shift to work correctly on a 64-bit system, use 1L instead of 1:

long j = 1L << i;