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
Why can templates only be implemented in the header file?
What is problem with overriding functions?
Which is most difficult programming language?
Is C++ case sensitive a) False b) Depends on implementation c) True
What is difference between c++ 11 and c++ 14?
Explain linear search.
What is c++ stringstream?
What are virtual constructors/destructors?
What do the header files usually contains?
What is friend class in c++ with example?
Write a Program for read a line from file from location N1 to N2 using command line arguments. Eg:exe 10 20 a.c
What is lambda in c++?
What is conditions when using boolean operators?
What are literals in C++?
What is #include iostream?