Udostępnij za pośrednictwem


C6297

warning C6297: Arithmetic overflow: 32-bit value is shifted, then cast to 64-bit value. Result may not be an expected value

This warning indicates incorrect behavior that results from integral promotion rules and types larger than those in which arithmetic is typically performed.

In this case, a 32-bit value was shifted left, and the result of that shift was cast to a 64-bit value. If the shift overflowed the 32-bit value, bits are lost.

If you do not want to lose bits, cast the value to be shifted to a 64-bit quantity before it is shifted. If you want to lose bits, performing the appropriate cast to unsigned long or a short type, or masking the result of the shift will eliminate this warning and make the intent of the code more clear.

Example

The following code generates this warning:

void f(int i)
{
  unsigned __int64 x;

  x = i << 34;
  // code 
}

To correct this warning, use the following code:

void f(int i)
{
  unsigned __int64 x;
  // code
  x = ((unsigned __int64)i) << 34;
}

See Also

Reference

Compiler Warning (level 1) C4293