MISRA C++:2008 Rule 5-8-1
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
Description
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.1
Rationale
When you use a shift operator, the value of the right-hand operand must be:
Greater than or equal to zero.
Less than the bit size of the data type of the left-hand operand.
If the above is not true, using a shift operator results in undefined behavior, which makes your program unpredictable and yields unexpected results.
Polyspace Implementation
Polyspace® reports a rule violation when you use a negative operand on the right-hand side of a shift operator. Polyspace also reports a violation when the right-hand side of a shift operator is at least as large as the bit size of the data type of the left-hand operand.
Troubleshooting
If you expect a rule violation but Polyspace does not report it, see Diagnose Why Coding Standard Violations Do Not Appear as Expected.
Examples
Check Information
| Group: Expressions |
| Category: Required |
Version History
Introduced in R2013b
1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.
The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:
MISRA C:2004
MISRA C:2012
MISRA C:2023
MISRA C++:2008
MISRA C++:2023
MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.