Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange implementation of Guava LongMath.checkedAdd [duplicate]

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 ||?

like image 871
ZhekaKozlov Avatar asked Nov 11 '22 23:11

ZhekaKozlov


1 Answers

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

like image 71
Louis Wasserman Avatar answered Nov 15 '22 12:11

Louis Wasserman