题解 | 二进制不同位数
二进制不同位数
https://www.nowcoder.com/practice/daf9032926614dab91ca624a7759a868
using System;
class Program {
static void Main() {
// 读取输入的两个整数
string[] parts = Console.ReadLine().Split();
int m = int.Parse(parts[0]);
int n = int.Parse(parts[1]);
// 第一步:异或运算,不同位会变成1
int x = m ^ n;
int count = 0;
// 第二步:统计异或结果中1的个数
while (x != 0) {
x &= x - 1; // 清除最右边的1
count++;
}
Console.WriteLine(count);
}
}