Integer.bitCount()的Java API告诉我们:
"public static int bitCount(int i)
返回指定int值的二进制补码表示形式中的一位数.此功能有时称为人口计数.
返回:指定int值的二进制补码表示形式中的一位数.自:1.5"
因此,如果我们取255并将其转换为二进制,我们得到11111111.如果我们将其转换为二进制补码版本,我们得到00000001,使得一位数为1.但是,如果我运行此代码:
import java.lang.*;
public class IntegerDemo {
public static void main(String[] args) {
int i = 255;
System.out.println("Number = " + i);
/* returns the string representation of the unsigned integer value
represented by the argument in binary (base 2) */
System.out.println("Binary = " + Integer.toBinaryString(i));
/* The next few lines convert the binary number to its two's
complement representation */
char[] tc= Integer.toBinaryString(i).toCharArray();
boolean firstFlipped = true;
for (int j …
Run Code Online (Sandbox Code Playgroud)