为什么“a ^ = b ^ = a ^ = b;”不同于“a ^ = b; b ^ = A;一个^ = B;&QUOT ;? [英] Why is "a^=b^=a^=b;" different from "a^=b; b^=a; a^=b;"?

查看:172
本文介绍了为什么“a ^ = b ^ = a ^ = b;”不同于“a ^ = b; b ^ = A;一个^ = B;&QUOT ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些代码来交换Java中的两个整数而不使用第三个变量,使用XOR。

I tried some code to swap two integers in Java without using a 3rd variable, using XOR.

以下是我尝试过的两个交换函数:

Here are the two swap functions I tried:

package lang.numeric;

public class SwapVarsDemo {

    public static void main(String[] args) {
        int a = 2984;
        int b = 87593;
        swapDemo1(a,b);
        swapDemo2(a,b);
    }

    private static void swapDemo1(int a, int b) {
        a^=b^=a^=b;
        System.out.println("After swap: "+a+","+b);
    }

    private static void swapDemo2(int a, int b) {
        a^=b;
        b^=a;
        a^=b;
        System.out.println("After swap: "+a+","+b);
    }

}

此代码产生的输出是这个:

The output produced by this code was this:

After swap: 0,2984
After swap: 87593,2984

我很想知道,为什么这句话:

I am curious to know, why is this statement:

        a^=b^=a^=b;

与此不同?

        a^=b;
        b^=a;
        a^=b;


推荐答案

问题是评估顺序:

参见 JLS第15.26.2节


首先,评估左侧操作数以生成变量。如果
此评估突然完成,则赋值表达式
突然完成,原因相同;右边的操作数不是
评估的,也不会发生任何分配。

First, the left-hand operand is evaluated to produce a variable. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the right-hand operand is not evaluated and no assignment occurs.

否则,保存左边操作数的值,然后保存
评估右手操作数。如果此评估突然完成
,那么赋值表达式突然完成
同样的原因并且没有赋值。

Otherwise, the value of the left-hand operand is saved and then the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

否则,保存的值为左侧变量和
的值右侧操作数用于执行复合赋值运算符指示的二元运算
。如果此操作
突然完成,则赋值表达式突然以
完成,原因相同并且没有赋值。

Otherwise, the saved value of the left-hand variable and the value of the right-hand operand are used to perform the binary operation indicated by the compound assignment operator. If this operation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

否则,结果为二进制运算转换为左手变量的
类型,受到值集转换(§5.1.13)
到相应的标准值集(不是扩展指数值
) set),并将转换结果存储到变量中。

Otherwise, the result of the binary operation is converted to the type of the left-hand variable, subjected to value set conversion (§5.1.13) to the appropriate standard value set (not an extended-exponent value set), and the result of the conversion is stored into the variable.

所以你的表达式确实如此:

So your expression does:

a ^ = b ^ = a ^ = b;


  1. 评估 a

  2. 评估 b ^ = a ^ = b

  3. xor这两个(所以第一步中的 a 没有 ^ = b 已申请)

  4. 将结果存储在 a

  1. evaluate a
  2. evaluate b^=a^=b
  3. xor the two (so the a in step one does not have ^=b applied to it yet)
  4. store the result in a

换句话说,你的表达式等同于以下java代码:

In other words, your expression is equivalent to the following java code:

    int a1 = a;
    int b2 = b;
    int a3 = a;
    a = a3 ^ b;
    b = b2 ^ a;
    a = a1 ^ b;

您可以从方法的反汇编版本中看到:

You can see that from the disassembled version of your method:

  private static void swapDemo1(int, int);
    Code:
       0: iload_0       
       1: iload_1       
       2: iload_0       
       3: iload_1       
       4: ixor          
       5: dup           
       6: istore_0      
       7: ixor          
       8: dup           
       9: istore_1      
      10: ixor          
      11: istore_0  

这篇关于为什么“a ^ = b ^ = a ^ = b;”不同于“a ^ = b; b ^ = A;一个^ = B;&QUOT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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