哪位高手来说说原理?
下边是我的一些测试:
55 54 52 48 32 0
(1 2 4 16 32)
99 98 96 64 0
(1 ,2,32,64)
// t.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int Cal1BitsNum(int x)
{
int countx=0;
while(x)
{
++countx;
x=x&(x-1);
}
return countx;
}
int _tmain(int argc, _TCHAR* argv[])
{
int x;
do
{
cin>>x;
cout<<Cal1BitsNum(x)<<endl<<"-----------"<<endl;
}while(1);
while(1);
return 0;
}