Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What is c++ course?

984


What is setbase c++?

1052


What is the use of map in c++?

1092


What is the c++ programming language used for?

987


What is lambda in c++?

1125


what are the types of Member Functions?

1082


How are the features of c++ different from c?

1016


What is the best way to declare and define global variables?

1225


what are Operators and explain with an example?

1120


what is pre-processor in C++?

1090


What are the methods of exporting a function from a dll?

1143


What is a linked list in c++?

999


How is data hiding achieved in c++?

1025


Can a program run without main?

1162


What's the order in which the objects in an array are destructed?

1337