Simplest way to check if two integers have same sign?

前端 未结 18 2550
礼貌的吻别
礼貌的吻别 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:43

    (integer1 * integer2) > 0

    Because when two integers share a sign, the result of multiplication will always be positive.

    You can also make it >= 0 if you want to treat 0 as being the same sign no matter what.

提交回复
热议问题