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

How do you initialize a string in c++?

1013


Please explain class & object in c++?

1105


Can we run c program in turbo c++?

1094


Why was c++ created?

1046


write a corrected statement in c++ so that the statement will work properly. x = y = z + 3a;

1908


What is atoi?

986


explain the reference variable in c++?

1088


What is time_t c++?

1149


What can I safely assume about the initial values of variables which are not explicitly initialized?

1071


What is an iterator class in c++?

1154


Can member functions be private?

1020


What is the auto keyword good for in c++?

1206


If you hear the cpu fan is running and the monitor power is still on, but you did not see anything show up in the monitor screen. What would you do to find out what is going wrong?

1149


What are the defining traits of an object-oriented language?

1218


Why should you learn c++?

1032