This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Strange Shift Warning

I am getting this warning:

warning C200: '>>': out of range shift factor has been truncated

with regards to this line of code:

Mbuf[1] = HEX2ASCII( (pEvent->num & 0xF000) >> 12 );

This warning does not appear when the identical project is compile on other machines!

Any ideas?

Rich

  • The error message leads me to think the compiler sees the LHS of the shift as a byte; a 12-bit shift would be "out of range". I'm not sure what other meaning "out of range" would have with respect to a shift.

    Perhaps the definition of the HEX2ASCII macro inclues a cast to a U8 (char of some sort). Check the parenthesization and precedence in the preprocessor output.

    Or, add your own parens, e.g.

    Mbuf[1] = HEX2ASCII((((U16)(pEvent->num) * 0xF000) >> 12));

    to make sure the integer widths are what you expect.

  • Drew,
    You were right, it was the "parenthesization"

    The macro:

    #define HEX2ASCII(b) ((b>9) ? (b+'A'-10) : (b+'0'))

    Fixed statement:

    Mbuf[1] = HEX2ASCII(( (pEvent->num & 0xF000) >> 12 ));

    Though I am not sure I see the correlation between the fix and the macro...

    Thank you
    Rich

  • The correlation is that that macro is written carelessly. To see what went wrong, inspect the preprocessor listing generated by the compiler on request, and ask yourself if that expanded expression really does what you wanted it to.

    The 'true' fix is to repair the macro definition, not the invocation:

    #define HEX2ASCII(b) (((b)>9) ? ((b)+'A'-10) : ((b)+'0'))
    And BTW, there almost certainly are more efficient ways of doing the job of this macro, including the very simple
    #define HEX2ASCII(b) digit_string[b]