Fastest way to check if two integers are on the same side of 0

后端 未结 6 1528
后悔当初
后悔当初 2021-02-02 10:02

I need to check if two integers are on the same side of zero many times. I don\'t care if it\'s positive or negative, just that it\'s the same side... and performance is very im

6条回答
  •  北恋
    北恋 (楼主)
    2021-02-02 10:39

    Another answer...

    final int i = int1 ^ int2;
    
    if (i == 0 && int1 == 0) {
        // both are zero
    } else if (i & Integer.MIN_VALUE == Integer.MIN_VALUE) {
        // signs differ
    } else {
        // same sign
    }
    

提交回复
热议问题