Give a very good method to count the number of ones in a "n"
(e.g. 32) bit number.
Answer Posted / sujan
#include<iostream>
#define bit 32
using namespace std;
int array[bit];
int bitConvert(int n)
{
int a,j=0;
a=n%2;
for(int i=bit;i>=0;i--)
{
n=n/2;
array[i]=a;
a=n%2;
}
for(int i=0;i<=bit;i++)
{
cout<<array[i];
}
}
int countBit(int a[])
{
int *ptr;
ptr=a;
int j=0;
for(int i=0;i<=bit;i++)
{
if(*ptr==1)
{
j++;
}
ptr++;
}
cout<< j;
}
int main()
{
int n;
cout<<"Enter the no:";
cin>>n;
cout<<"\n"<<"BitConversion of "<<n<< "is:";
bitConvert(n);
cout<<endl<<endl;
cout<<"\n"<<"No. of bit:";
countBit(array);
system("pause");
}
| Is This Answer Correct ? | 4 Yes | 1 No |
Post New Answer View All Answers
program explaining feautures of c++
What is the disadvantage of using a macro?
Differentiate between realloc() and free().
Write about all the implicit member functions of a class?
What is the use of dot in c++?
What is function overriding in c++?
What are enumerations?
What is the use of endl?
Explain selection sorting?
Explain the static storage classes in c++.
Does c++ have a hash table?
What is friend class in c++ with example?
What is recursion?
What data structure is fastest, on average, for retrieving data: a) Binary Tree b) Hash Table c) Stack
Write about the stack unwinding?