奇怪的实现Guava LongMath.checkedAdd

时间:2013-08-16 12:52:12

标签: java guava long-integer integer-overflow

public static long checkedAdd(long a, long b) {
    long result = a + b;
    checkNoOverflow((a ^ b) < 0 | (a ^ result) >= 0);
    return result;
}

我感兴趣为什么布尔逻辑|在这里使用。为什么不使用条件短路||?

1 个答案:

答案 0 :(得分:1)

该班的第一个评论:

// NOTE: Whenever both tests are cheap and functional, it's faster to use 
// &, | instead of &&, ||

更多背景信息:https://stackoverflow.com/a/11412121/869736