Java逻辑符号 [英] Notation for logic in Java
问题描述
我在Google上很难找到一个绝对基本的Java问题.以下是什么意思:
Absolutely basic Java question which I'm having a hard time finding on Google. What does the following mean:
(7 & 8) == 0?
那等同于写作:
7 == 0 || 8 == 0?
我写了一个快速主干来测试这一点,事实似乎是这样.我只是想确保自己没有遗漏任何东西.
I wrote a quick main which tests this, and it seems to be the case. I just wanted to make sure I'm not missing anything.
推荐答案
不是. &
是按位和.如果在两个输入中都设置了相应的位,则它将设置一个位.由于在二进制中,7是111
,8是1000
,因此它们没有共同的位,因此结果是0
.
Nope. &
is bitwise and. It sets a bit if the corresponding bits are set in both inputs. Since in binary, 7 is 111
and 8 is 1000
, they have no bits in common, so the result is 0
.
对于您建议的内容,实际上并没有任何速记语法,只能一行显示.有几种解决方法-测试Set
或BitSet
中的成员资格,使用switch
语句-但没有什么比7 == 0 || 8 == 0
既高效又短.
There isn't really any shorthand syntax for the thing you suggest, not on a single line. There are a few workarounds -- test for membership in a Set
or BitSet
, use a switch
statement -- but nothing that's both as efficient and as short as just 7 == 0 || 8 == 0
.
这篇关于Java逻辑符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!