题解 | #二进制中1的个数#
二进制中1的个数
https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8
public class Solution {
public int NumberOf1(int n) {
int res = 0;
for (int i = 0; i < 32; i++) {
if ((n & (1 << i)) != 0) res++;
}
return res;
}
}
解题思想:位知识
* &: 只有都为1才为1
* |: 只要有一个为1就为1
#算法##算法笔记#