乘一次去掉最右边一个1.
package com.oracle.test;
import org.testng.annotations.Test;
public class ConvertIntTest {
int func(int x) {
int count = 0;
while(x!=0) {
count ++;
x = x & (x-1);
}
return count;
}
@Test
void testConvertInt() {
System.out.println(func(6666));
}
}
输出如下,
图片.png