Give a method to count the number of ones in a 32 bit number?

Answers were Sorted based on User's Feedback



Give a method to count the number of ones in a 32 bit number?..

Answer / ataraxic

int i = 0;

while (x) {
x &= x-1;
++i;
};

printf("number of ones is %d\n", i);

Is This Answer Correct ?    1 Yes 0 No

Give a method to count the number of ones in a 32 bit number?..

Answer / vignesh1988i

#include<stdio.h>
#include<conio.h>
void main()
{
unsigned i;
int j=0,count=0;;
printf("Enter the number :");
scanf("%ld",&i);
while(j<=31)
{
if(!(((i>>j)&1)^1))
count++;
j++;
}
printf("\nnumber of 1's in ur number is : %d",count);
getch();
}


thank u

Is This Answer Correct ?    0 Yes 0 No

Give a method to count the number of ones in a 32 bit number?..

Answer / jayaprakash

#include<stdio.h>
#include<conio.h>

main()
{
int i;
int n;
int count=0;
int j;
int res=0;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(j=15;j>=0;j--)
{ i=1;
i=i<<j;
res=i&n;
if(res!=0)
count++;
}
printf("\nNumber of ones is:%d",count);


getch();

}

Is This Answer Correct ?    0 Yes 0 No

Give a method to count the number of ones in a 32 bit number?..

Answer / jayaprakash

#include<stdio.h>
#include<conio.h>

main()
{
int i;
int n;
int count=0;
int j;
int res=0;
clrscr();
printf("Enter the number:");
scanf("%d",&n);
for(j=15;j>=0;j--)
{ i=1;
i=i<<j;
res=i&n;
if(res!=0)
count++;
}
printf("\nNumber of ones is:%d",count);


getch();

}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

write program on arrays

3 Answers   GE, Polycab,


Tell us the use of fflush() function in c language?

0 Answers  


Why cann't whole array can be passed to function as value.

1 Answers  


Which programming language is best for getting job 2020?

0 Answers  


What are data structures in c and how to use them?

0 Answers  






What is sizeof return in c?

0 Answers  


Which is more efficient, a switch statement or an if else chain?

0 Answers  


What is the difference between constant pointer and pointer to a constant. Give examples.

4 Answers   TCS,


Simplify the program segment if X = B then C &#8592; true else C &#8592; false

0 Answers  


c program to add and delete an element from circular queue using array

3 Answers  


What is meant by errors and debugging?

0 Answers  


What is c preprocessor mean?

0 Answers  


Categories