10.3 and essential type of & expression

Moderators: misra-c, david ward

Post Reply
lovewar
Posts: 10
Joined: Thu Feb 17, 2011 8:01 am
Company: .

10.3 and essential type of & expression

Post by lovewar » Thu Oct 15, 2015 6:54 am

Would the following examples be compliant with rule #10.3?

Code: Select all

void foo(void) {
  uint8_t u8a = getCode();
  
  sint32_t s32a = u8a & 0xFF;    // here 
}
Would the following examples be non-compliant with rule #10.3?

Code: Select all

void foo(void) {
  uint8_t u8a = getCode();
  
  uint8_t u8b = u8a & 0xFF;    // here
}

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

Re: 10.3 and essential type of & expression

Post by misra-c » Thu Oct 29, 2015 3:08 pm

This discussion assumes the following types.

Code: Select all

typedef unsigned char uint8_t;
typedef signed int sint32_t;
and that signed short has 16 bits.

The essential type of u8a is "unsigned char", whereas the essential type of 0xFF is "signed short" (see Appendix D.6)
Therefore "u8a & 0xFF" will violate both
Rule 10.1 RH operand of & is signed
and
Rule 10.4 both operands do not have the same essential type category

Appendix D.7 explains that if both operands do not have the same signedness the resulting essential type of the operation is the standard C type for that operation.

According to the C standard the usual arithmetic operations are performed on the operands.
u8a is promoted to a C standard type of "signed int" - assuming the above type definitions
0xFF has a C standard type of "signed int"

Therefore both the resultant C standard type and the essential type is "signed int".

Code: Select all

sint32_t s32a = u8a & 0xFF;
The assignment will occur from the essential type of "signed int" to "signed int", which is compliant with 10.3.

Code: Select all

uint8_t u8b = u8a & 0xFF;
An assigning conversion will occur from the essential type of "signed int" to "unsigned char". This violates rule 10.3 because the conversion is to a type of a different essential type category.
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “8.10 The essential type model”