alt

其实第1种情况是第2种情况的特例,只用第2种情况的证明,不管i是否为1逻辑上也是OK的。


class Solution {
public:
    int  NumberOf1(int n) {
         unsigned int v1 = n;
		 int cnt = 0;
		 while (v1)
		 {
			 cnt++;
			 v1 = v1 & (v1 - 1);
		 }
		 
		 return cnt;
     }
};