Skip to main content

Program to find the number of 1's bit in a number



#include<bits/stdc++.h>
using namespace std;
// this is the program to find the number of 1's bit in a number
int count1nsBit(int n)
{
    int count=0;
    while (n)
    {
        n= n & n-1;
        count++;
    }
    return count;
}

int main()
{
    int n;
    cin>>n;
    cout<<count1nsBit(n);
    return 0;
}
/*
input an output tast are comments
*/

Comments

  1. input: 7
    output: 3

    input: 8
    output: 1

    input: 512
    output: 1

    input: 999
    output: 8

    ReplyDelete

Post a Comment

Popular posts from this blog

Array Data Structure

7 features of OOPs

The 7 features of OOPs are as follows: 1. Incapsulation 2. Abstraction 3. Inheritance 4. Polymorphism 5. Object must be used 6. Massage passing : One object can interect with another object 7. Dynamic binding The most initial 4 are the basic OOPs features that is required.