Simplest way to check if two integers have same sign?

前端 未结 18 2551
礼貌的吻别
礼貌的吻别 2020-12-04 13:53

Which is the simplest way to check if two integers have same sign? Is there any short bitwise trick to do this?

18条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 14:50

    assuming 32 bit

    if(((x^y) & 0x80000000) == 0)

    ... the answer if(x*y>0) is bad due to overflow

提交回复
热议问题