剑指offer——面试题15.2:判断两个整数m和n的二进制中相差多少位

 1 #include"iostream"
 2 using namespace std;
 3 
 4 int CountDifferentBit(int m,int n)
 5 {
 6     int cnt=0,diff=m^n;
 7     while(diff)
 8     {
 9         cnt++;
10         diff=(diff-1)&diff;
11     }
12     return cnt;
13 }
14 
15 int main()
16 {
17     int m,n;
18     while(cin>>m>>n)
19     {
20         cout<<CountDifferentBit(m,n)<<endl;
21     }
22     return 0;
23 }
View Code

 

posted @ 2019-02-19 18:46  Run_For_Love  阅读(229)  评论(0编辑  收藏  举报