Java 一个整数的二进制位有多少个1,哪些位是1
int x = 122;
int count = 0;
while(x != 0)
{
int z = x;
x = x & ( x - 1 );
//此为1的索引
z = Integer.toBinaryString(z-x).length()-1;
//count为有多少位1
count++;
}
int count = 0;
while(x != 0)
{
int z = x;
x = x & ( x - 1 );
//此为1的索引
z = Integer.toBinaryString(z-x).length()-1;
//count为有多少位1
count++;
}