美图 2018校招 后端研发工程师在线考试-编程题一

题目 Bit位数计算

描述

两个 int32 整数 m 和 n 的二进制表达,计算有多少个位 (bit) 不同?

输入

一行中给定两个数字

输出

输出这两个数字中bit不同的个数

Example

Input

15 8

Output

3

题解

思路

  • 两数异或
  • 统计 1 的个数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>

using namespace std;

int m,n;

int main()
{
while(cin>>m>>n)
{
m^=n;
int count=0;
while(m)
{
m&=m-1;
++count;
}
cout<<count<<endl;
}
}