bitCount()
이라는 함수를 부호없는 정수 인수의 이진 표현에서 비트 수를 반환하는 bitcount.c
파일에 작성하고 싶습니다. 여기 부호없는 정수의 비트 수를 계산하십시오.
#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 */
}
좋아, 내가 얻을 :
# 1-bits in base 2 representation of 0 = 1, should be 0
# 1-bits in base 2 representation of 1 = 56, should be 1
# 1-bits in base 2 representation of 2863311530 = 57, should be 16
# 1-bits in base 2 representation of 536870912 = 67, should be 1
# 1-bits in base 2 representation of 4294967295 = 65, should be 32
RUN SUCCESSFUL (total time: 14ms)
그것은 비트의 정확한 숫자를 반환하지 않습니다.
C의 부호없는 정수 인수의 이진 표현에서 비트 수를 반환하는 가장 좋은 방법은 무엇입니까?
'bitCount()'에서 당신은 무엇을 시도 했습니까? – ogzd
"여기에 코드가 없습니다"라고 생각합니다. –
'__builtin_popcount'를 사용할 수 있습니까? – harold