XOR异或在Java代码中怎么写
在Java中,你可以使用^运算符来实现XOR(异或)操作。下面是一个简单的例子:
public class XORExample {
public static void main(String[] args) {
int a = 10; // 二进制表示:1010
int b = 7; // 二进制表示:0111
int result = a ^ b; // XOR操作
System.out.println("XOR结果: " + result); // 输出:XOR结果: 13 (二进制表示:1101)
}
}
在这个例子中,我们定义了两个整数a和b,然后使用^运算符对它们进行异或操作。最后,我们将结果输出到控制台。