title: MISRA - Prohibited cast of complex integer expression: Signed versus Unsigned
body: <p>I'm at wits end. I can't get MISRA to stop complaining.</p>
  
  <pre><code>typedef unsigned char T_u8;
  typedef signed char T_char;
  T_u8 value = 0u;
  T_char out[some_length];
  </code></pre>
  
  <p>Begin</p>
  
  <pre><code>out[size_out--] = (( value | 0x30) );
  scm.c  320  Note 960: Violates MISRA 2004 Required Rule 12.7, Bitwise operator applied to signed underlying type: |
  scm.c  320  Info 734: Loss of precision (assignment) (8 bits to 7 bits)
  </code></pre>
  
  <p>Sure. Makes sense. I will change it so I am OR'ing the same type.</p>
  
  <pre><code>out[size_out--] = (( value | 0x30u) );
  scm.c  320  Info 713: Loss of precision (assignment) (unsigned int to char)
  </code></pre>
  
  <p>So, with the OR expression, the expression is being promoted to an unsigned int. So, I should cast it to the type it is expecting.</p>
  
  <pre><code>out[size_out--] = (T_char)(( value | 0x30u) );
  scm.c  320  Note 960: Violates MISRA 2004 Required Rule 10.3, Prohibited cast of complex integer expression: Signed versus Unsigned
  </code></pre>
  
  <p>Now what? I don't get it. Why can't I cast it? What do I have to do?</p>
  
  <pre><code>out[size_out--] = (T_char)((T_u8)( (T_u8)value | (T_u8)0x30u) );
  scm.c  320  Note 960: Violates MISRA 2004 Required Rule 10.3, Prohibited cast of complex integer expression: Signed versus Unsigned
  </code></pre>
  
  <p>This didn't help:</p>
  
  <pre><code>out[size_out--] = (( (T_u32)value | (T_u32)0x30) );
  scm.c  320  Info 713: Loss of precision (assignment) (unsigned long to char)
  </code></pre>