Warning C6297
Arithmetic overflow: 32-bit value is shifted, then cast to 64-bit value. Result may not be an expected value
Remarks
This warning indicates incorrect behavior that results from integral promotion rules and types larger than the ones 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 don't want to lose bits, cast the value to shift to a 64-bit quantity before it's shifted. If you want to lose bits, perform the appropriate cast to unsigned long
or a short
type. Or, mask the result of the shift. Either approach eliminates this warning and makes the intent of the code clearer.
Code analysis name: RESULTOFSHIFTCASTTOLARGERSIZE
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 = static_cast<unsigned __int64>(i) << 34;
}