Print an integer in binary format in Java

2019-01-02 14:33发布

I have a number and I want to print it in binary. I don't want to do it by writing an algorithm, Is there any built-in function for that in Java?

标签: java
17条回答
呛了眼睛熬了心
2楼-- · 2019-01-02 15:08

check out this logic can convert a number to any base

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));
    }
}

OR

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());
查看更多
余欢
3楼-- · 2019-01-02 15:08

There are already good answers posted here for this question. But, this is the way I've tried myself (and might be the easiest logic based → modulo/divide/add):

        int decimalOrBinary = 345;
        StringBuilder builder = new StringBuilder();

        do {
            builder.append(decimalOrBinary % 2);
            decimalOrBinary = decimalOrBinary / 2;
        } while (decimalOrBinary > 0);

        System.out.println(builder.reverse().toString()); //prints 101011001
查看更多
荒废的爱情
4楼-- · 2019-01-02 15:08

Binary representation of given int x with left padded zeros:

org.apache.commons.lang3.StringUtils.leftPad(Integer.toBinaryString(x), 32, '0')
查看更多
梦醉为红颜
5楼-- · 2019-01-02 15:10

Assuming you mean "built-in":

int x = 100;
System.out.println(Integer.toBinaryString(x));

See Integer documentation.

(Long has a similar method, BigInteger has an instance method where you can specify the radix.)

查看更多
姐姐魅力值爆表
6楼-- · 2019-01-02 15:11

Simply try it. If the scope is only printing the binary values of given integer value. It can be positive or negative.

    public static void printBinaryNumbers(int n) {
    char[] arr = Integer.toBinaryString(n).toCharArray();
    StringBuilder sb = new StringBuilder();
    for (Character c : arr) {
        sb.append(c);
    }
    System.out.println(sb);
}

input

5

Output

101

查看更多
无色无味的生活
7楼-- · 2019-01-02 15:11

It works with signed and unsigned values used powerful bit manipulation and generates the first zeroes on the left.

public static String representDigits(int num) {

        int checkBit = 1 << (Integer.SIZE * 8 - 2 );    // avoid the first digit        
        StringBuffer sb = new StringBuffer();

        if (num < 0 ) {     // checking the first digit
            sb.append("1");
        } else {
            sb.append("0");
        }

        while(checkBit != 0) {          
            if ((num & checkBit) == checkBit){
                sb.append("1");
            } else {
                sb.append("0");
            }           
            checkBit >>= 1;     
        }       

        return sb.toString();
    }
查看更多
登录 后发表回答