[previous] 4130 [next] Bitwise operations on signed data will give implementation defined results.
Arithmetic type - Operations REFERENCE - ISO:C90-6.3 Expressions

It is not usually appropriate or portable to perform bitwise operations (|, &, ^ , ~, <<, >>) on expressions of signed type because the representation of negative integer numbers is implementation-defined (although usually two's complement).

Bitwise operations are appropriate to data which represents a set of bits rather than a numeric value and in practice this implies an unsigned data type.

A special situation arises when expressions of small integer type are the operands of a bitwise operator. The rules of integral promotion require that these objects are often promoted to type signed int before the bitwise operation, and so internally the operation is performed on a signed data type even when the underlying type is unsigned.

For example:


/*PRQA S 2017,2100,3120,3198,3199,3203,3210,3408,3447,3602 ++*/


extern unsigned int uia;
extern unsigned int uib;

extern signed int   sia;
extern signed int   sib;

extern unsigned int uca;
extern unsigned int ucb;


extern void foo(void)
{
    unsigned int  uir;
    signed int    sir;
    unsigned char ucr;

    uir = uia & uib;
    uir = uia | uib;
    uir = uia ^ uib;
    uir = ~uia;
    uir = uia << 5;
    uir = uia >> 3;

    ucr = (unsigned char)(uca & ucb);
    ucr = (unsigned char)(uca | ucb);
    ucr = (unsigned char)(uca ^ ucb);
    ucr = (unsigned char)(~uca);
    ucr = (unsigned char)(uca << 5);
    ucr = (unsigned char)(uca >> 3);

    sir = sia & sib;                    /* Message 4130 */
    sir = sia | sib;                    /* Message 4130 */
    sir = sia ^ sib;                    /* Message 4130 */
    sir = ~sia;                         /* Message 4130 */
    sir = sia << 5;                     /* Message 4131 */
    sir = sia >> 3;                     /* Message 0502 */


}


No MISRA C:2012 Rules applicable to message 4130


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