[previous] 2862 [next] Apparent: Implementation-defined value resulting from left shift operation on expression of signed type.
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 */
}

See also:

QA·C Source Code Analyser 8.1.2
© 2013 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index Contents