/* This program's aim is to count the number of bits set in an integer */
#include<stdio.h>
int count=0;
int check(int);
int main()
{
int i,r;
char ch;
printf("enter the integer for which you want to check the bits set");
scanf("%d", &i);
r=check(i);
printf("the number of occurance of 1 in the integer is %d \n", r); /*don't know why isit printing 0 when i give 4 as input */
return 0;
}
int check(int j)
{
if((j & 1)==1)
count++;
for(int l=0;l<31;l++)
{
if(j>>1 & 1)
count++;
}
return count;
}
이 프로그램에 어떤 문제가 있습니까? 어리석은 실수 나 개념적인 것처럼 보입니다. 또한 j >> 1로 작성해야합니까? 우리는 단순히 j >>를 쓸 수 없습니까?왜이 비트 조작 프로그램이 잘못된 O/P를 제공합니까?
정말 코드 서식을 개선해야합니다. – harold
들여 쓰기를 수정하고 언어 태그 –
을 추가하십시오. 다른 구현 : http://stackoverflow.com/q/109023 – harold