unsigned becomes signed in if-statement comparisons?

后端 未结 4 948
不思量自难忘°
不思量自难忘° 2020-12-17 23:51

I have searched this site for an answer and found many responses to unsigned/signed comparison but this problem is that only unsigned parameters are compared but still it wo

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-18 00:22

    In the expression:

    if( (u16_varLow - u16_varHigh) > (unsigned short)5 )  
    

    (u16_varLow - u16_varHigh) will be promoted to an int and evaluate to -65525. The fix for your problem is to cast to an unsigned type, like you do in the "Does enter"-code.

    The reason s16_Res1 = u16_varLow - u16_varHigh; yields 11 is that the result of the subtraction, -65525, doesn't fit in a short.

提交回复
热议问题