[previous] 0502 [next] A right shift on signed data may be an arithmetic or a logical shift.
Arithmetic type - Operations

A right shift operation is being performed on an operand whose underlying type is signed. If the value is negative, the result will be implementation-defined.

A right shift operation on signed data may be performed in two different ways - depending on the compiler.

The behaviour of QAC may be configured to conform to the behaviour of the compiler in this respect using the -arithrsh option. However it is usually considered unwise to perform any sort of shift operation on signed data. Message 0502 identifies a right shift operation and message 4131 identifies a left shift operation.

A special situation arises when an expression of a small unsigned integer type is the left hand operand of a shift operator. Expressions of this type are subject to integral promotion and so although the underlying data is unsigned the result type will be signed int. QAC does not generate message 0502 when the underlying type of the operand is unsigned.

Finally. notice that the shifting right of a negative twos-complement integer may not produce the same result as a division by two. The result of dividing the value -5 by 2 can be implemented as -2 remainder -1, or as -3 remainder 1. An arithmetic right shift will aways yield the value -2.

For example:


/*PRQA S 2017,2100,3120,3198,3408,3447 ++*/

extern int           sia;
extern unsigned char uca;

extern void foo(void)
{
    sia = sia >> 3;        /* Message 0502 */
    sia = uca >> 3;        /* No message   */

    sia = sia << 3;        /* Message 4131 */
    sia = uca << 3;        /* No message */
}


No MISRA-C:2004 Rules applicable to message 0502


See also:

QA·C Source Code Analyser 8.1
MISRA-C:2004 Compliance Module 3.2
© 2012 Programming Research.
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents