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 |
how would a 4*3 array A[4][3] stored in Row Major Order?
Explain what is #line used for?
Why we use stdio h in c?
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?
Explain c preprocessor?
What are loops c?
HOW TO SOLVE A NUMERICAL OF LRU IN OS ??????
Can we change the value of static variable in c?
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.
Why does everyone say not to use gets?
What is the symbol indicated the c-preprocessor?
Explain how can I right-justify a string?