标签:style class blog code java http
#include<stdio.h> void main(){ unsigned x = 0645; // 110_100_101 int a = bitcount(x); printf("%d\n",a); } /*bitcount:获得x(二进制)中1的个数*/ int bitcount(unsigned x){ int b = 0; for(;x != 0; x >>= 1){ if(x & 1){ b++; } } return b; }
解题思路:对于x的二进制形式,分为两种:最右边二进制位为0和最右边二进制位为1
代码实现:
#include <stdio.h> void main() { unsigned x = 0645; // 110_100_101 int count = 0; while (x != 0) { x &= (x-1); count++; } printf("1-bit count: %d\n", count); }
标签:style class blog code java http
原文地址:http://www.cnblogs.com/ningvsban/p/3781210.html