2013-08-16 3 views
5
public static long checkedAdd(long a, long b) { 
    long result = a + b; 
    checkNoOverflow((a^b) < 0 | (a^result) >= 0); 
    return result; 
} 

私はなぜ論理的な論理的な|ここで使用されます。なぜ条件付き短絡||を使用しないのですか?Guavaの奇妙な実装LongMath.checkedAdd

+0

あなたはそれを書かれているだろうか? –

+0

checkNoOverflow((a^b)<0 ||(a^result)> = 0); – ZhekaKozlov

+3

性能上の理由から分岐を避けたいコードでは珍しいことではありません。 – molbdnilo

答えて

1

そのクラスの最初のコメント:

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

もっとコンテキスト:https://stackoverflow.com/a/11412121/869736

関連する問題