Title description
Enter an integer and output the number of 1s in the binary representation of the number. Negative numbers are represented by complements.
For the topic of test position calculation, here is a link about the concept of simple bit calculation:
https://github.com/CyC2018/CS-Notes/blob/master/notes/Leetcode%20%E9%A2%98%E8%A7 % A3% 20-% 20% E4% BD% 8D% E8% BF% 90% E7% AE% 97.md
Code:
class Solution { public : int NumberOf1 ( int n) { int count = 0 ; int flag = 1 ; / * while (flag! = 0) { if ((n & flag)! = 0) { count ++; } flag = flag << 1; // flag == 0 means it has traversed all the bits of the binary representation of int type } * / while (n! = 0 ) { count ++ ; n & = n- 1 ; } return count; } };