Search This Blog

Sunday, 11 December 2016

Count number of ones in binary of a number


#include <iostream>
using namespace std;
//count no of ones in binary representation
int count1(int n)
{
    int c=0;
    while(n)
    {
        n=n&(n-1);
        cout<<n;
        c++;
    }
    return c;
}
int main(){
 cout<<count1(6);
 return 0;
}

No comments:

Post a Comment