题解 | #两个整数二进制位不同个数#
两个整数二进制位不同个数
https://www.nowcoder.com/practice/16e48900851646c0b2c6cdef9d7ea051
#include <stdio.h> int RecordDifference(int num1, int num2) { //用异或记录两个整数二进制格式在每一位上的差异:1 不同,0 相同 int record = num1 ^ num2; //统计record 位上的1 int count = 0;//记录record位上的1的个数 while(record) { record &= record - 1; count++; } return count; } int main() { int num1 = 0; int num2 = 0; scanf("%d %d", &num1, &num2); printf("%d", RecordDifference(num1, num2)); return 0; }