#include "stdafx.h"
#include "stdio.h"
#include <stdlib.h>
#include <process.h>
int main(int argc, char* argv[])
{
int countx = 0,
x=9991;
char szBinary[64]={0};
printf("please input an int data\n",countx);
scanf("%d",&x);
itoa(x,szBinary,2);
printf("x=%d\t%s\n",x,szBinary);
while(x)
{
countx ++;
x = x&(x-1);
itoa(x,szBinary,2);
printf("x=%d\t%s\n",x,szBinary);
}
printf("countx=%d\n",countx);
system("pause");
// printf("press any key to quit\n",countx);
return 0;
}
算法固然好,但是不很直观哦
其实我觉得只要转化成字符串之后,直接找里面"1"字符就可以了,同样的扩展性更好,可以转化成随意进制,找随意数字的个数 .