You have lots of problems in this code.
- The specific warning would probably be about the operands of
|
being uint8
but you assign them to a wider unsigned type uint16
. In general, you need to study Implicit type promotion rules if you are to attempt MISRA compliance. The MISRA document itself is also good study material regarding this.
myArray_B[idx *2]<<8
is regardless of MISRA dangerous code, since there is a silent promotion to a signed type. I recommend to always convert to a large, unsigned integer type before using bit shifts.
- Similarly, the
|
is carried out on promoted operands that are signed.
myArray_A[idx] = ... idx++
is the FAQ beginner bug, invoking undefined behavior. You cannot write code like that in C. Why are these constructs using pre and post-increment undefined behavior?.
- Modifying a loop iterator from inside a for loop is horrible practice and a violation of MISRA-C 14.2.
- Don't use home-brewed integer types, use
stdint.h
.
- MISRA requires
u
suffix on integer constants that are meant to be unsigned (doesn't matter the slightest in this specific case though).
To fix the bugs and get MISRA-C compliance, you should rewrite it into something along the lines of this:
uint16_t myArray_A[5];
uint8_t myArray_B[10] = {0x1u,0x2u,0x3u,0x4u,0x5u,0x6u,0x7u,0x8u,0x9u,0x10u};
for (uint8_t i=0u; i<5u; i++)
{
myArray_A[i] = ((uint16_t)myArray_B[i*2u] << 8u) |
((uint16_t)myArray_B[i*2u + 1u]);
}