给出统计一个字节中被置1的位的个数的函数,要求效率尽可能高

 1 // 一个字节中置1的位数.cpp 
 2 
 3 #include "stdafx.h"
 4 
 5 int bit1count(char x)
 6 {
 7     int count=0;
 8     while(x)
 9     {
10         x&=(x-1);//去掉最右边的1
11         count++;
12     }
13     return count;
14 }
15 
16 void main()
17 {
18     int num=0;
19     char ch;
20     printf("input ch:\n");
21     scanf("%d",&ch);
22     printf("ch=%d\n",ch);
23     num=bit1count(ch);
24     printf("%d\n",num);
25 }

 

 

相关文章:

  • 2022-12-23
  • 2021-05-14
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-16
  • 2021-07-14
猜你喜欢
  • 2022-12-23
  • 2021-11-23
  • 2022-01-10
  • 2022-12-23
  • 2021-05-30
  • 2022-12-23
  • 2021-04-19
相关资源
相似解决方案