Re: C and MISRA, blues... (arithmetic shifts)



Nils wrote:
I'm currently rewriting some numerical code for MISRA compliance.

Signed shifts are not defined by the C-standard, and the code-checker complaints. Well - no big surprise here. I knew that and did it nevertheless. Now I have to rewrite.


But do you do if you need them anyway? I need *lots* of them, so in despair I've just created this monster of unreadable code:


How about:

int ArithmeticShiftRight (int value, unsigned int shift)
{
if (value < 0) {
return -((int) (((unsigned int) (-value)) >> shift);
} else {
return ((int) (((unsigned int) (value)) >> shift);
}
}

It's still horrible, but it probably compiles to better code.
.



Relevant Pages