write a program to find out number of on bits in a number?
Answers were Sorted based on User's Feedback
Answer / rajkumar
#include<stdio.h>
void main()
{
int a,count=0;
while(a>0)
{
if(a%2==1)
count++;
a=a>>1;
}
printf("no of on bits =%d ",count);
}
Is This Answer Correct ? | 15 Yes | 1 No |
Answer / vivek
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 ? | 23 Yes | 12 No |
main()
{
int n,counter;
printf("enter a number");
scanf("%d",&n);// let n=5
while(n>0)
{
counter++;
n=n&(n-1);
}
printf("%d",counter);
getch();
}
Is This Answer Correct ? | 11 Yes | 2 No |
Answer / krishna kanth
#include<stdio.h>
main()
{
int setbit=1;
int number=16;//for example
int numBitSet=0;
clrscr();
while(setbit<=number)//important and optimized condition
{
if(number&setbit)
numBitSet++;
setbit=setbit<<1;
}
printf("%d",numBitSet);
getch();
}
Is This Answer Correct ? | 10 Yes | 3 No |
Answer / anilkumar
int n ; \\any number
for(count=0;n&=(n-1); count++);
printf("%d", count);
Is This Answer Correct ? | 11 Yes | 4 No |
Answer / vaishu and hema
main()
{
int n,count=0,rem;
scanf("%d",&n);
while(n>0)
{
rem=n%2;
if(rem==1)
count++;
n=n/2;
}
printf("The number of on bits in the number is %d",count);
}
Is This Answer Correct ? | 7 Yes | 3 No |
Answer / naveen
//plz let me know whether it is correct or not...bcoz am just beginner
#include<stdio.h>
void main()
{
int j=0,i=0xFFFFE,temp=1;
int count=0;
while(j<=(sizeof(int)*8-1))
{
if(i&temp)
count++;
temp=temp<<1;
j++;
}
printf("%d",count);
}
Is This Answer Correct ? | 3 Yes | 0 No |
Answer / jaya j
unsigned int count1s(unsigned char x)
{
int p;
for(p=0;x!=0;x>>=1)
if(x&1)
p++;
return p;
}
Is This Answer Correct ? | 2 Yes | 0 No |
Answer / madhu
#include <stdio.h>
main()
{
int number;
int setbit=1; //Lets start checking from first bit
int numBitSet=0; //Number of bits set in the number
printf("enter the number");
scanf("%d", &number);
while(setbit>0)
{
if(number&setbit) //bit wise and
numBitSet++;
setbit=setbit<<1;
}
printf("%d",numBitSet);
}
full program of the first ans...
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / ajay vikram
void main()
{
int number,a=0,b,count=0;
printf("Enter the number : ");
scanf("%d",&number);
b = (number/2);
while(b)
{
if((number>>a)& 1)
{
count++;
}
a++;
b--;
}
printf("Number of ON Bits in the number : %d\n",count);
}
Is This Answer Correct ? | 1 Yes | 0 No |
Explain output of printf("Hello World"-'A'+'B'); ?
what is unsigened char and what is the difference from char
In c programming language, how many parameters can be passed to a function ?
what is used instead of pointers in java than c?
Are there constructors in c?
What is class and object in c?
What is the mean of this statement:: if(int i=0 * i=9)
Why pointers are used in c?
What are Storage Classes in C ?
32 Answers CTS, HP, IBM, Maharaja Whiteline, Tamil Nadu Open University TNOU, TATA, TCS, Wipro,
How can I recover the file name given an open stream?
There are 8 billiard balls, and one of them is slightly heavier, but the only way to tell was by putting it on a weighing scale against another. What's the fewest number of times you'd have to use the scale to find the heavier ball?
what will be the output of this program? void main() { int a[]={5,10,15}; int i=0,num; num=a[++i] + ++i +(++i); printf("%d",num); }