You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

27 lines
597 B

2 years ago
package class32;
public class Problem_0191_NumberOf1Bits {
// n的二进制形式有几个1
public static int hammingWeight1(int n) {
int bits = 0;
int rightOne = 0;
while(n != 0) {
bits++;
rightOne = n & (-n);
n ^= rightOne;
}
return bits;
}
public static int hammingWeight2(int n) {
n = (n & 0x55555555) + ((n >>> 1) & 0x55555555);
n = (n & 0x33333333) + ((n >>> 2) & 0x33333333);
n = (n & 0x0f0f0f0f) + ((n >>> 4) & 0x0f0f0f0f);
n = (n & 0x00ff00ff) + ((n >>> 8) & 0x00ff00ff);
n = (n & 0x0000ffff) + ((n >>> 16) & 0x0000ffff);
return n;
}
}