位运算计数(day2) 发表于 2016-09-20 | 题目: 求整数的2进制表示中1的个数整数 右移 相与123456789int function1(int i) { int count = 0; while(i) { if(i & 1) count ++; i = i >> 1; } return count;} flag 左移 相与1234567891011int function2(int i) { int count = 0; unsigned int flag = 1; while(flag){ if(i & flag){ count ++; } flag = flag << 1; } return count;} 减一相与12345678int function3(int i) { int count = 0; while (i) { ++ count; i = (i - 1) & i; } return count;} 源码github 赏 微信打赏 支付宝打赏