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

how would a 4*3 array A[4][3] stored in Row Major Order?

0 Answers   HCL, Ignou,


Explain what is #line used for?

0 Answers  


Why we use stdio h in c?

0 Answers  


You have an array of n integers, randomly ordered with value 1 to n-1.The array is such that there is only one and one value occurred twice. How will you find this number?

1 Answers  


Explain c preprocessor?

0 Answers  






What are loops c?

0 Answers  


HOW TO SOLVE A NUMERICAL OF LRU IN OS ??????

0 Answers  


Can we change the value of static variable in c?

0 Answers  


You are given a string which contains some special characters. You also have set of special characters. You are given other string (call it as pattern string). Your job is to write a program to replace each special characters in given string by pattern string. You are not allowed to create new resulting string. You need to allocate some new memory to given existing string but constraint is you can only allocate memory one time. Allocate memory exactly what you need not more not less.

2 Answers   Microsoft,


Why does everyone say not to use gets?

0 Answers  


What is the symbol indicated the c-preprocessor?

0 Answers  


Explain how can I right-justify a string?

0 Answers  


Categories