class Solution { public: int NumberOf1(int n) { int ans=0 ; unsigned int flag = 1 ; while(flag){//flag超出范围之后会是0 if(n&flag)ans++; flag = flag<<1 ; } return ans ; } };
【剑指offer11二进制中1的个数】
原文:https://www.cnblogs.com/Stephen-Jixing/p/13124110.html