gpt4 book ai didi

java - Bit wise or Operator works in different method 的详细解释

转载 作者:行者123 更新时间:2023-11-30 07:16:15 25 4
gpt4 key购买 nike

这里是代码。

public class Test {

public static void main(String[] args) {
int x=2; int y = 3;
if((y == x++) | (x < ++y)){
System.out.println("x = " + x + "Y = " + y);
}
}
}

这输出 x=3 y=4

另一个标准

public class Test {

public static void main(String[] args) {
System.out.println(4|3);
}
}

这输出 7

这里第二个条件 | 用作位运算符。但在第一个标准中 | 不是。它操纵并给出输出。我需要解释第一个标准是如何工作的。

我知道的是,| 将比较两侧,而 || 将只比较左侧,这是真的,它会继续下一步。

希望我的问题很清楚。提前谢谢你..

最佳答案

对于第一个示例,您将拥有:

if ((3 == 2) | (3 < 4))

等于:

if ( false | true )

并计算为 true(因为比较了表达式的两边)。

对于第二个例子,因为你已经知道 | 会比较双方,下面是它真正发生的事情。将数字转换为二进制,然后应用操作:

4 | 3

将转换为:

100 | 011 = 111

等于7

关于java - Bit wise or Operator works in different method 的详细解释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17163666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com