【发布时间】:2013-02-08 13:40:02
【问题描述】:
在 bitcount.c 中编写一个名为 bitCount() 的函数,它返回 1 的位数 其无符号整数参数的二进制表示。记得填写身份证明 信息并运行完成的程序以验证正确性。
/*
Name:
Lab section time:
*/
#include <stdio.h>
int bitCount (unsigned int n);
int main ( ) {
printf ("# 1-bits in base 2 representation of %u = %d, should be 0\n",
0, bitCount (0));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
1, bitCount (1));
printf ("# 1-bits in base 2 representation of %u = %d, should be 16\n",
2863311530u, bitCount (2863311530u));
printf ("# 1-bits in base 2 representation of %u = %d, should be 1\n",
536870912, bitCount (536870912));
printf ("# 1-bits in base 2 representation of %u = %d, should be 32\n",
4294967295u, bitCount (4294967295u));
return 0;
}
int bitCount (unsigned int n) {
/* your code here */
}
有人可以帮我准确理解这是在问什么吗? bitCount 是不是要把输入的十进制转换成二进制,然后计算 1 的个数?
【问题讨论】:
-
Bit Twiddling Hacks 页面上列出了很多方法。
标签: binary