Rule 12.7 and underlying type

6.12 Expressions

Moderators: misra-c, david ward

Locked
aravind
Posts: 2
Joined: Tue Apr 12, 2005 7:14 pm
Location: USA

Rule 12.7 and underlying type

Post by aravind » Fri Jun 30, 2006 10:48 pm

Hi,

Question on 12.7: Bitwise operators shall not be applied to operands whose underlying type is signed

Code: Select all

int32_T    var1[32]; 
uint16_T  var2 = 5U;
int32_T    var3 = var1[(var2 - 1) << 2];
Is the use of left-shift on expression (var2- 1) compliant with Rule 12.7?
What is the underlying type of the expression (var2 -1)?

Will it be compliant if a \"U\" suffix is added to the constant, e.g.

Code: Select all

int32_T    var3 = var1[(var2 - 1U) << 2];
Thanks
Aravind

misra-c
Posts: 566
Joined: Thu Jan 05, 2006 1:11 pm

Post by misra-c » Tue Aug 22, 2006 5:21 pm

MISRA-C meeting 22-8-2006

The array index sub-expression

Code: Select all

var2 - 1
contains both unsigned and signed values (respectively) and is prohibited under rule 10.1.

The underlying type of a mixed signed / unsigned expression is undefined.

In your second example, the array index sub-expression

Code: Select all

var2 - 1U
is unsigned, and therefore the shift can be applied.
---
Posted by and on behalf of
the MISRA C Working Group

Locked

Return to “6.12 Expressions”