获得一个int数中二进制位为1 的个数
 
int NumberOfOne(int n){
                 int count = 0;
                 unsigned int flag = 1;
                 while (flag){
                                 if (n & flag){
                                                count++;
                                }
                                flag = flag << 1;
                }
                 return count;
}
int NumberOfOne2(int n){
                 int count = 0;
                 while (n )
                {
                                count++;
                                 n = n &(n - 1);
                }
                 return count;
}
posted on 2013-11-08 16:46  追梦的飞飞  阅读(205)  评论(0编辑  收藏  举报