世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?

/**
* 世界上有10种人,一种懂二进制,一种不懂。那么你知道两个int32整数m和n的二进制表达,有多少个位(bit)不同么?
* 输入例子:
* 1999 2299
*


* 输出例子:
* 7
*/
public class XiaoMi {

public static void test(int m, int n) {

   //  异或
    int k = m ^ n;
    int count = 0;
    for (int i = 0; i < 32; i++) {

        if (((k >> i) & 1) != 0)
            count++;

    }
    System.out.println(count);
}

public static void main(String[] args) {
    test(1999, 2299);
}

}

猜你喜欢

转载自blog.csdn.net/qq_38844040/article/details/80696180