正题
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a
;
scanf("%d",&a
);
char b
[32];
itoa(a
,b
,2);
printf("该数的二进制表示为:%s\n",b
);
int n1_1
=0;
for(int i
=0;i
<32;i
++)
{
if((a
&(1<<i
))==(1<<i
))
n1_1
++;
}
printf("第一种方法结果为%d\n",n1_1
);
int n1_2
=0;
for(int i
=0;i
<32;i
++)
{
if(((a
>>i
)&1)==1)
n1_2
++;
}
printf("第一种方法变式结果为%d\n",n1_2
);
int n2
=0;
for(;a
!=0;)
{
a
=(a
-1)&a
;
n2
++;
}
printf("第二种方法结果为%d\n",n2
);
}
转载请注明原文地址:https://tech.qufami.com/read-17319.html