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

What is a const pointer?

0 Answers  


How #define works?

0 Answers  


int a=0,b=2; if (a=0) b=0; else b=*10; What is the value of b ?

6 Answers   TCS,


An organised method of depicting the use of an area of computer memory used to signify the uses for different parts of the memory a) swap b) extended memory c) memory map d) all of the above

0 Answers  


What is this infamous null pointer, anyway?

0 Answers  






What are the phases in s/w developed life cycle? wat is the diff b/w stack & queue...where do we use stack

6 Answers  


What are file streams?

0 Answers  


#include<stdio.h> void main() { int a [5]; for (i=0; i<=4; i++) printf(ā€œ%dā€ ,a[i]); }

8 Answers  


What is the value of h?

0 Answers  


what is c?

7 Answers   Tech Mahindra,


Reverse the part of the number which is present from position i to j. Print the new number.[without using the array] eg: num=789876 i=2 j=5 778986

2 Answers  


What is the auto keyword good for?

0 Answers  


Categories