This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
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;