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:
int ArithmeticShiftRight (int value, unsigned int shift) { if (shift) { /* Get unsigned version of value to work with */ unsigned int x = (unsigned int) value;
/* replicate the sign-bit of value into all bits of SignMask: */ unsigned int SignMask = ((unsigned int) -(value < 0));
/* assemble the arithmetic shift from two unsigned shifts. */ x = (x>>shift)|(SignMask