[previous] MISRA-C:2004  Rule  12.8:  (Required) [next] The right hand operand of a shift operator shall lie between zero and one less than the width in bits of the underlying type of the left hand operand.
If, for example, the left hand operand of a left-shift or right-shift is a 16-bit integer, then it is important to ensure that this is shifted only by a number between 0 and 15 inclusive.

See section 6.10 for a description of underlying type.

There are various ways of ensuring this rule is followed. The simplest is for the right hand operand to be a constant (whose value can then be statically checked). Use of an unsigned integer type will ensure that the operand is non-negative, so then only the upper limit needs to be checked (dynamically at run time or by review). Otherwise both limits will need to be checked. s

u8a  = ( uint8_t ) ( u8a << 7 );                 /* compliant     */
u8a  = ( uint8_t ) ( u8a << 9 );                 /* not compliant */
u16a = ( uint16_t )( ( uint16_t ) u8a << 9 );    /* compliant     */


QAC messages that encompass this guideline:

0499 Right operand of shift operator is greater than or equal to the width of the essential type of the left operand.
2790 Constant: Right hand operand of shift operator is negative or too large.



(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