문제

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

I am interested why boolean logical | is used here. Why not to use conditional short circuited ||?

도움이 되었습니까?

해결책

The first comment in that class:

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

More context: https://stackoverflow.com/a/11412121/869736

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top