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 C?
two progs are given. one starts counting frm 0 to MAX and the other stars frm MAX to 0. which one executes fast.
Explain what are binary trees?
What is a macro, and explain how do you use it?
What are the features of the c language?
how to convert binary to decimal and decimal to binary in C lanaguage
7 Answers BPO, Far East Promotions, IBM, RBS,
who is the editor of 'pokemon'?
What is a structure in c language. how to initialise a structure in c?
the maximum width of a c variable name can be a) 6 characters b) 8 characters c) 10 characters d) 20 characters
Explain the use of 'auto' keyword in c programming?
Find if a number is power of two or not?
write the function int countchtr(char string[],int ch);which returns the number of timesthe character ch appears in the string. for example the call countchtr("she lives in Newyork",'e') would return 3.