[previous] 0499 [next] Right operand of shift operator is greater than or equal to the width of the essential type of the left operand.
Arithmetic type - Operations REFERENCE - ISO:C90-6.3.7 Bitwise Shift Operators - Semantics

An operand whose underlying type is smaller than int, is undergoing a shift operation. The right hand operand of the shift operation is a constant value which is greater than or equal to the width in bits of the underlying type.

When a shift operation is performed on an operand of type signed int or unsigned int (or a larger type), it is only possible to shift by an amount which is less than the width in bits of the type of the left hand operand. Shifting by any greater amount will result in undefined behavior. This condition is identified by message 2790.

Message 0499 identifies the situation where the value of the shift is less than the width of the standard type but is greater than or equal to the width of the underlying type. An operation of this type is only meaningful for a left shift and only because of the effect of integral promotion. It will not result in undefined behaviour but the effect of such an operation needs to be clearly understood.

In the following example, type char is assumed to be 8 bits wide and type int is assumed to be 32 bits wide.


/*PRQA S 1-9999 ++*/
/*PRQA S 499, 2790 --*/

unsigned int  ui;
unsigned char uc;

extern void foo(void)
{
    ui = uc << 6;
    ui = uc << 7;
    ui = uc << 8;               /* Message 0499 */
    ui = uc << 9;               /* Message 0499 */
    ui = uc << 30;              /* Message 0499 */
    ui = uc << 31;              /* Message 0499 */
    ui = uc << 32;              /* Message 2790 */
    ui = uc << 33;              /* Message 2790 */

    ui = ui << 6;
    ui = ui << 7;
    ui = ui << 8;
    ui = ui << 9;
    ui = ui << 30;
    ui = ui << 31;
    ui = ui << 32;              /* Message 2790 */
    ui = ui << 33;              /* Message 2790 */

    ui = uc >> 6;
    ui = uc >> 7;
    ui = uc >> 8;               /* Message 0499 */
    ui = uc >> 9;               /* Message 0499 */
    ui = uc >> 30;              /* Message 0499 */
    ui = uc >> 31;              /* Message 0499 */
    ui = uc >> 32;              /* Message 2790 */
    ui = uc >> 33;              /* Message 2790 */

    ui = ui >> 6;
    ui = ui >> 7;
    ui = ui >> 8;
    ui = ui >> 9;
    ui = ui >> 30;
    ui = ui >> 31;
    ui = ui >> 32;              /* Message 2790 */
    ui = ui >> 33;              /* Message 2790 */
}

See also:

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