用Java打印二进制格式的整数

aks*_*xyz 254 java

我有一个数字,我想用二进制打印.我不想通过编写算法来实现它,Java中是否有任何内置函数?

Jon*_*eet 410

假设你的意思是"内置":

int x = 100;
System.out.println(Integer.toBinaryString(x));
Run Code Online (Sandbox Code Playgroud)

请参阅整数文档.

(Long有一个类似的方法,BigInteger有一个实例方法,你可以指定基数.)

  • 这省略了领先的0 (2认同)

Moh*_*Ali 238

这里不需要只依赖于二进制或任何其他格式......一个灵活的内置函数可用于打印你想要的程序格式.Integer.toString(int,representation);

Integer.toString(100,8) // prints 144 --octal representation

Integer.toString(100,2) // prints 1100100 --binary representation

Integer.toString(100,16) //prints 64 --Hex representation
Run Code Online (Sandbox Code Playgroud)

  • `toBinaryString`使用两个补码输出,`toString`转换指定基数并在前面放一个负号,所以`toString(-8,2)=="-1000"` (8认同)
  • 不适用于负数. (5认同)

ada*_*shr 61

System.out.println(Integer.toBinaryString(343));
Run Code Online (Sandbox Code Playgroud)


M2X*_*M2X 21

我需要一些东西来很好地打印出来并将每个n位的位分开.换句话说,显示前导零并显示如下内容:

n = 5463
output = 0000 0000 0000 0000 0001 0101 0101 0111
Run Code Online (Sandbox Code Playgroud)

所以这就是我写的:

/**
 * Converts an integer to a 32-bit binary string
 * @param number
 *      The number to convert
 * @param groupSize
 *      The number of bits in a group
 * @return
 *      The 32-bit long bit string
 */
public static String intToString(int number, int groupSize) {
    StringBuilder result = new StringBuilder();

    for(int i = 31; i >= 0 ; i--) {
        int mask = 1 << i;
        result.append((number & mask) != 0 ? "1" : "0");

        if (i % groupSize == 0)
            result.append(" ");
    }
    result.replace(result.length() - 1, result.length(), "");

    return result.toString();
}
Run Code Online (Sandbox Code Playgroud)

像这样调用它:

public static void main(String[] args) {
    System.out.println(intToString(5463, 4));
}
Run Code Online (Sandbox Code Playgroud)

  • @Parth 在每一步中,我们都会在字符串末尾附加一个空格字符(“”)。您提到的行将删除最后一个附加的空格字符。本质上,您可以调用“String”类的“trim”函数来达到相同的效果。 (2认同)

Sey*_*lal 10

public static void main(String[] args) 
{
    int i = 13;
    short s = 13;
    byte b = 13;

    System.out.println("i: " + String.format("%32s", 
            Integer.toBinaryString(i)).replaceAll(" ", "0"));

    System.out.println("s: " + String.format("%16s", 
            Integer.toBinaryString(0xFFFF & s)).replaceAll(" ", "0"));

    System.out.println("b: " + String.format("%8s", 
            Integer.toBinaryString(0xFF & b)).replaceAll(" ", "0"));

}
Run Code Online (Sandbox Code Playgroud)

输出:

i: 00000000000000000000000000001101
s: 0000000000001101
b: 00001101
Run Code Online (Sandbox Code Playgroud)


Dav*_*ams 7

老套:

    int value = 28;
    for(int i = 1, j = 0; i < 256; i = i << 1, j++)
        System.out.println(j + " " + ((value & i) > 0 ? 1 : 0));
Run Code Online (Sandbox Code Playgroud)


小智 6

这是打印整数内部二进制表示最简单方法例如:如果我们将 n 设为 17,那么输出将是:0000 0000 0000 0000 0000 0000 0001 0001

void bitPattern(int n) {

        int mask = 1 << 31;
        int count = 0;
        while(mask != 0) {
            if(count%4 == 0)
                System.out.print(" ");

            if((mask&n) == 0) 

                System.out.print("0");



            else 
                System.out.print("1");


            count++;
            mask = mask >>> 1;


    }
    System.out.println();
}
Run Code Online (Sandbox Code Playgroud)


Zah*_*Ali 5

检查出这个逻辑可以将数字转换为任何基数

public static void toBase(int number, int base) {
    String binary = "";
    int temp = number/2+1;
    for (int j = 0; j < temp ; j++) {
        try {
            binary += "" + number % base;

            number /= base;
        } catch (Exception e) {
        }
    }
    for (int j = binary.length() - 1; j >= 0; j--) {
        System.out.print(binary.charAt(j));
    }
}
Run Code Online (Sandbox Code Playgroud)

要么

StringBuilder binary = new StringBuilder();
int n=15;
while (n>0) {
    if((n&1)==1){
        binary.append(1);
    }else
        binary.append(0);
    n>>=1;
}
System.out.println(binary.reverse());
Run Code Online (Sandbox Code Playgroud)