当前位置:编程学习 > C/C++ >>

计算9999的二进制中含有多少个1

print?// 求数字的二进制含有多少个1.cpp : Defines the entry point for the console application.  
//  
 
#include "stdafx.h"  
#include <stdio.h>  
int func(int num) 

int countx = 0; 
while (num) 

countx ++; 
num = num&(num-1); 

return countx; 

int _tmain(int argc, _TCHAR* argv[]) 

    int num=9999; 
    int count=func(9999); 
    printf("%d",count); 
    getchar(); 
    return 0; 

// 求数字的二进制含有多少个1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
int func(int num)
{
int countx = 0;
while (num)
{
countx ++;
num = num&(num-1);
}
return countx;
}
int _tmain(int argc, _TCHAR* argv[])
{
 int num=9999;
 int count=func(9999);
 printf("%d",count);
 getchar();
 return 0;
}


 

补充:软件开发 , C++ ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,