在bitcount.c中寫入名爲bitCount()的函數,返回其無符號整數參數的二進制表示形式的 中的1位數。請記住填寫標識 信息並運行完成的程序以驗證正確性。解釋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](http://graphics.stanford.edu/~seander/bithacks.html#CountBitsSetNaive)頁面列出了大量的方法。 – 2013-02-08 13:44:07