java & 运算符有两个整数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20646700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:19:51  来源:igfitidea点击:

java & operator with two integers?

javabitwise-operatorsbitwise-and

提问by nife552

From what I understand the & operator is similar to the && operator except that the && only checks the second if the first is true, while the & checks both regardless of the result of the first one. Basically the && just saves a little time and power.

据我了解,& 运算符与 && 运算符类似,不同之处在于 && 仅在第一个为真时才检查第二个,而无论第一个的结果如何,& 都会检查两者。基本上 && 只是节省了一点时间和力量。

If that is so, then how does this code work?

如果是这样,那么这段代码是如何工作的?

int l = 0;
if ((l & 8) != 0 && (l & 4) == 0){ do something}

what does the (l & 8)and the (l & 4)do? What does the & do in this case?

什么是(l & 8)(l & 4)呢?& 在这种情况下做什么?

回答by Raffaele Rossi

& and && are two different operators but the difference is not what you've described.

& 和 && 是两个不同的运算符,但区别不是您所描述的。

& does the bit-wise AND of two integers and produces a third integer whose bit are set to 1 if both corresponding bits in the two source integers are both set to 1; 0 otherwise.

& 对两个整数进行按位与运算,如果两个源整数中的相应位都设置为 1,则生成第三个整数,该整数的位设置为 1;0 否则。

&& applies only to two booleans and returns a third boolean which will be true if both the input booleans are true; false otherwise.

&& 仅适用于两个布尔值并返回第三个布尔值,如果两个输入布尔值都为真,则该值为真;否则为假。