![]() |
![]() |
2862 | ![]() |
||||
![]() | |||||||
| Shift operations | |||||||
An apparent anomaly has been detected. Preceding operations imply that the result of this shift operation will sometimes be implementation-defined. The result of a left shift operation will probably be meaningless when the value is negative or when it is positive and of a magnitude such that the sign bit may be set by the shift operation. In general, shift operations on expressions of signed type should be avoided. In the code below, the size of a long is assumed to be 32 bits. If the value of
sl is greater than or equal to 0x10000000L, any operation which shifts left by more than 2 bits may generate a result which is negative.
/*PRQA S 295,1253,1277,1279,2017,2982,2983,2984,2986,3120,3198,3199,3203,3227,3408,4131 ++*/
void foo(long sl)
{
long slr;
if (sl > 0x10000000L)
{
}
slr = sl << 2; /* */
slr = sl << 3; /* 2862 */
slr = sl << 4; /* 2862 */
}
MISRA C:2012 Rules applicable to message 2862:
| Dir-1.1 (Required) | Any implementation-defined behaviour on which the output of the program depends shall be documented and understood |
See also:
![]() | ||
| QA·C Source Code Analyser 8.1
MISRA C:2012 Compliance Module 1.0 © 2012 Programming Research. www.programmingresearch.com | Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index | Contents |