public
static
int
getBitCount(
int
n)
...
{
int count = 0;
boolean isNegative = false;
if(n < 0) ...{
n = -n;
isNegative = true;
}
while (n > 0) ...{
count+=(n & 1);
n>>=1;
}
return isNegative ? count + 1 : count;
}
方案2:
public static int getBitCount( int n) ... {
int count = 0;
while (n != 0) ...{
count+=(n & 1);
n>>>=1;
}
return count;
}
int count = 0;
boolean isNegative = false;
if(n < 0) ...{
n = -n;
isNegative = true;
}
while (n > 0) ...{
count+=(n & 1);
n>>=1;
}
return isNegative ? count + 1 : count;
}
方案2:
public static int getBitCount( int n) ... {
int count = 0;
while (n != 0) ...{
count+=(n & 1);
n>>>=1;
}
return count;
}