Give a method to count the number of ones in a 32 bit number?
Answers were Sorted based on User's Feedback
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 |
#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 |
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 |
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 |
What is a const pointer?
How #define works?
int a=0,b=2; if (a=0) b=0; else b=*10; What is the value of b ?
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
What is this infamous null pointer, anyway?
What are the phases in s/w developed life cycle? wat is the diff b/w stack & queue...where do we use stack
What are file streams?
#include<stdio.h> void main() { int a [5]; for (i=0; i<=4; i++) printf(ā%dā ,a[i]); }
What is the value of h?
what is c?
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
What is the auto keyword good for?