Skip to content

Latest commit

 

History

History
50 lines (36 loc) · 1.52 KB

c6297.md

File metadata and controls

50 lines (36 loc) · 1.52 KB
title description ms.date f1_keywords helpviewer_keywords ms.assetid
Warning C6297
Describes causes of MSVC Code analysis warning C6297, and how to fix the issue.
07/15/2020
C6297
RESULTOFSHIFTCASTTOLARGERSIZE
__WARNING_RESULTOFSHIFTCASTTOLARGERSIZE
C6297
17b585f0-75e5-4fc0-935a-143ec67659f4

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;
}

See also

Compiler Warning (level 1) C4293