[previous] MISRA-C:2004  Rule  12.7:  (Required) [next] Bitwise operators shall not be applied to operands whose underlying type is signed.
Bitwise operations (~, <<, <<=, >>, >>=, &, &=, ^, ^=, | and |=) are not normally meaningful on signed integers. Problems can arise if, for example, a right shift moves the sign bit into the number, or a left shift moves a numeric bit into the sign bit.

See section 6.10 in the MISRA-C:2004 document for a description of underlying type.

Example Code:


#include "misra.h"
#include "m2cmex.h"


extern S16 test_1207( void )
{
   s16a = (S16)(s16a << 2);  /* MISRA Violation */
   s16a = s16a >> 1;         /* MISRA Violation */
   s16a = s16a & 3;          /* MISRA Violation */
   s16a = s16a | 5;          /* MISRA Violation */
   s16a = (S16)~s16a;        /* MISRA Violation */

   u8a = (U8)(u8a << 2);
   u8a = u8a >> 1;
   u8a = u8a & 0x3U;
   u8a = u8a | 0x5U;
   u8a = (U8)~u8a;

   return 1;
}


QAC messages that encompass this guideline:

4532 An expression of 'essentially signed' type (%1s) is being used as the %2s operand of this bitwise operator (%3s).
4533 An expression of 'essentially signed' type (%1s) is being used as the left-hand operand of this shift operator (%2s).



(c) The Motor Industry Research Association, 2004
QA C Source Code Analyser 8.1.2
MISRA-C:2004 Compliance Module 3.2
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA-C:2004 Rule Index Contents