write a program to find out number of on bits in a number?
Answers were Sorted based on User's Feedback
Answer / minisha
int count(unsigned int n)
{
int c=0;
while(n)
{
n=n&(n-1);
c++
}
return c;
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / arijit
#include<stdio.h>
#include<conio.h>
void main()
{
int n,cnt,rem
scanf("%d",&n);
cnt=0;
while(n!=0)
{
rem=n%2;
n=n/2;
cnt++;
}
printf("number of bits of the number is = %d",cnt);
getch();
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / ram
#include<stdio.h>
void main()
{
int a,count=0;
printf("enter a");
scanf("%d",&a);
while(a>0)
{
if(a%2==1)
count++;
a=a>>1;
}
printf("no of on bits =%d ",count);
}
Is This Answer Correct ? | 0 Yes | 0 No |
Answer / kavitha
int setbit=1; //Lets start checking from first bit
int numBitSet=0; //Number of bits set in the number
while(setbit>0)
{
if(number&setbit) //bit wise and
numBitSet++;
setbit=setbit<<1;
}
Is This Answer Correct ? | 0 Yes | 1 No |
Answer / anu
#include<stdio.h>
#include<conio.h>
void main()
{
int n,cnt,rem
scanf("%d",&n);
cnt=1;
while(n!=0)
{
rem=n%2;
n=n/2;
cnt++;
}
printf("number of bits of the number is = %d",cnt);
getch();
}
Is This Answer Correct ? | 0 Yes | 3 No |
Answer / sriharsha
main()
{
int n;
printf("\n Enter The Number Whose bits have to find");
scanf("%d",&n);
i=i*8;
printf("\n The number of bits in the given number is %d",i);
}
Is This Answer Correct ? | 2 Yes | 8 No |
Answer / jaskarann
int i,a;
{
cout<<enter the no whose bits you want to find";
cin>>i;
a=i*8;
cout<<No. of bits in this number is<<a;
getch();
Is This Answer Correct ? | 5 Yes | 36 No |
What is a pointer variable in c language?
Write a program to find the smallest and largest element in a given array in c language
A function 'q' that accepts a pointer to a character as argument and returns a pointer to an array of integer can be declared as: A)int (*q(char*)) [] B)int *q(char*) [] C)int(*q)(char*) [] D)None of the Above
What is the use of bitwise operator?
In C, What is the #line used for?
write a program that finds the factorial of a number using recursion?
What is the purpose of 'register' keyword in c language?
What should malloc() do? Return a null pointer or a pointer to 0 bytes?
Why isnt there a numbered, multi-level break statement to break out
what is the output of following question? void main() { int i=0,a[3]; a[i]=i++; printf("%d",a[i] }
There is a 100-story building and you are given two eggs. The eggs (and the building) have an interesting property that if you throw the egg from a floor number less than X, it will not break. And it will always brake if the floor number is equal or greater than X. Assuming that you can reuse the eggs which didn't broke; you got to find X in a minimal number of throws. Give an algorithm to find X in minimal number of throws.
What is the best style for code layout in c?