Java Integer to Binary Conversion

JavaJavaBeginner
Practice Now

Introduction

In this lab, we will learn how to convert an integer to binary in Java. There are several methods to do this conversion, and we will cover the most commonly used methods.

Using toBinaryString() method

The toBinaryString() method is the easiest way to convert an integer to a binary string. This method is an Integer class method that returns a String after converting the int to binary.

public static String toBinaryString(int val)

Here val is the value to which we want to convert to the binary number system. This method will return the binary value in string format.

public class IntegerToBinaryConverter {
    public static void main(String[] args) {
        int val = 183;
        System.out.println("Value in binary system is: " + Integer.toBinaryString(val));
    }
}

Output: Value in binary system is: 10110111

Using Long Division Method

This method is completely mathematical. We declare an array of an integer of size 32 by considering 32-bit binary representation. Each time we divide a number by 2 and store the reminder inside the array of the integer. In the end, to get the result we traverse it in a reverse way.

public class IntegerToBinaryConverter {
    public static void main(String[] args) {
        int val = 183;
        int num[] = new int[32];
        int pos = 0;
        while (val > 0) {
            num[pos++] = val % 2;
            val = val / 2;
        }
        System.out.print("Value in binary system is: ");
        for (int i = pos - 1; i >= 0; i--) {
            System.out.print(num[i]);
        }
    }
}

Output: Value in binary system is: 10110111

Using Bit Manipulation

Using the right shift operator we can make any value into half, and since we are working on a bit level it is a very low-cost operation; and the rest of the things are the same as mentioned in the above example. At last, we are printing value using reverse the value which is stored in a StringBuilder object to make it in the binary form correctly.

public class IntegerToBinaryConverter {
    public static void main(String[] args) {
        int val = 183;
        StringBuilder binary = new StringBuilder(32);
        while (val > 0) {
            binary.append(val % 2);
            val >>= 1;
        }
        binary.reverse().toString();
        System.out.print("Value in binary system is: " + binary.reverse().toString());
    }
}

Output: Value in binary system is: 10110111

Using Integer.toString() Method

There is one more interesting way in java.lang.Integer class by using Integer.toString() method, which accepts the first argument as a number and the second as a radix (here radix is a base of number system) which can be 2 for Binary, 8 for Octal, 16 for Hexadecimal; in our case, we will set the radix to 2 for Binary number.

public class IntegerToBinaryConverter {
    public static void main(String[] args) {
        int val = 183;
        System.out.print("Value in binary system is: " + Integer.toString(val, 2));
    }
}

Output: Value in binary system is: 10110111

Summary

In this lab, we learned how to convert an integer to a binary number system in Java using four different methods. The first method is using the built-in toBinaryString() method, the second one using a long division method, the third one is using bit manipulation, and the fourth one is using Integer.toString() method.

Each method has its own advantages and disadvantages, and you can use the one that suits best according to your needs.

Other Java Tutorials you may like