按位异或(^)

import org.junit.Test;

/**
 * ^ : 按位异或
 * ------------------------------------
 * 按位异或,比较每个操作数的二进制位,相同置为0,不同置为1
 */
public class Demo {

    @Test
    public void testName() throws Exception {
        System.out.println(101 ^ 101); // 0
        System.out.println(112 ^ 112); // 0
        System.out.println(101 ^ 112); // 21
        System.out.println(112 ^ 101); // 21

        /*
         * 101 ^ 112的值为什么是21?
         * 逻辑:按位异或,比较每个操作数的二进制位,相同置为0,不同置为1
         * -------------------------------------
         * 0110 0101    // 101
         * 0111 0000    // 112
         * ----------
         * 0001 0101    // 21
         */
        System.out.println(Integer.toBinaryString(101)); // 1100101
        System.out.println(Integer.toBinaryString(112)); // 1110000
        System.out.println(Integer.toBinaryString(21)); // 10101
    }
}
原文地址:https://www.cnblogs.com/zj0208/p/8031594.html