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 |
write program on arrays
Tell us the use of fflush() function in c language?
Why cann't whole array can be passed to function as value.
Which programming language is best for getting job 2020?
What are data structures in c and how to use them?
What is sizeof return in c?
Which is more efficient, a switch statement or an if else chain?
What is the difference between constant pointer and pointer to a constant. Give examples.
Simplify the program segment if X = B then C ← true else C ← false
c program to add and delete an element from circular queue using array
What is meant by errors and debugging?
What is c preprocessor mean?