HUAWEI Written Test Question: Find the number of 1 when an int positive integer is stored in memory

 

Title description

Enter a positive integer of type int and calculate the number of 1s when the int data is stored in memory.

Enter description:

 Enter an integer (int type)

Output description:

 After this number is converted to binary, the number of 1 is output

Example 1

Input

5

Output

2
#include <iostream>

using namespace std;


int main() {
    int n;
    cin >> n;
    int num = 0;
    while (n) {
        if (n & 1) num++;
        n = n >> 1;
    }
    cout << num << endl;
    return 0;
}

 

 
Published 34 original articles · Like 10 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/weixin_41111088/article/details/104793045