现在的位置: 首页 > 综合 > 正文

C语言的一道面试题(35)

2013年10月10日 ⁄ 综合 ⁄ 共 585字 ⁄ 字号 评论关闭

/************************************************************
 Copyright (C), 1998-2006, Rx. Co., Ltd.
 FileName: mainApp.cpp
 Author: longronglin
 Version : 1.0
 Date: 2006-10-10
 Description:     
 Function List:  
    1. int main()
 History:     
      <author> <time> <version > <desc>
      longronglin    2006/10/10     1.0      modify xxx .
***********************************************************/

#include <stdio.h>
/*
 * 返回1为正确执行
 * 主函数
 *
 */
int func(int x)
{
    int countx = 0;
    while(x)
    {
          countx ++;
          x = x&(x-1);
     }
    return countx;
}

int main()
{
 
 printf("%d",func(9999));

 return (1);
}  

答案是8。为9999转换为2进制1的个数。 

抱歉!评论已关闭.