Java逻辑符号 [英] Notation for logic in Java

查看:100
本文介绍了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.

对于您建议的内容,实际上并没有任何速记语法,只能一行显示.有几种解决方法-测试SetBitSet中的成员资格,使用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆