小编Jon*_*ing的帖子

为什么Integer.bitCount()为255的输入返回8?

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)

java bitcount

2
推荐指数
1
解决办法
3703
查看次数

标签 统计

bitcount ×1

java ×1