image.png

    1. #include <iostream>
    2. using namespace std;
    3. int main()
    4. {
    5. int num_10 = 0;
    6. int num_test = 0x01;
    7. int result = 0;
    8. cin >> num_10;
    9. for (int i = 0; i < 32; i++)
    10. {
    11. //注意这里的括号必须加,否则优先级错误
    12. if((num_10&num_test)!=0)
    13. {
    14. result++;
    15. }
    16. //移位后记得赋值
    17. num_test = num_test<<1;
    18. }
    19. cout << result << endl;
    20. }