简体   繁体   中英

Why do I receive a warning for1I64<<31?

Why do I receive a warning for this code ?

switch(iInput)
{
   ...
   case 1I64<<31:   return 31;  break;
   ...
}

1>C:\\path-to-file.cpp(44) : warning C4309: 'case' : truncation of constant value

1I64<<31 is 0x0000000080000000 (__int64) so no truncation here, it's there a maximum value for case ?

The type of the expression controlling the switch controls they type of the expression the case labels will use.

You'll need an __int64 (or equivalent) type in the controlling expression to get rid of the warning.

The C Standard ( n1256.pdf ) states

6.8.4.2/5 The integer promotions are performed on the controlling expression. ...

and

6.3.1.1/2 ... the value is converted to an int; otherwise, it is converted to an unsigned int ...

My guess is that int (or unsigned int ) in your implementation is not large enough to hold 64-bit-wide values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM