Are there good uses for non-short-circuiting logical (boolean) operators in Java/Scala?

后端 未结 4 1625
栀梦
栀梦 2020-12-07 00:42

I recently discovered that Java (and Scala) include non-short-circuiting logical operators &, |, and ^. I previously thought thes

4条回答
  •  无人及你
    2020-12-07 01:21

    Hmm. I know they can be incredibly useful for optimizing C/C++ code if used carefully. It might apply to Java as well.

    The major use in C -- other than actual bit operations -- is to remove a pipeline stall. The short circuit operators require a branch operation. The bitwise operator will compute both sides and removes the chance for a mispredicted branch and the resulting stall.

提交回复
热议问题