Signup/Sign In
PUBLISHED ON: JANUARY 28, 2021

Java Integer to Binary Conversion

In this article, we will be talking about integer to binary conversion in Java. Usually, there are two ways to convert an integer to the binary number system.

  • toBinaryString()
  • Mathematical long division method

Firstly we will learn about the toBinaryString() method. It 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. and then this method will return the binary value in string format.

Example: Int to Binary Conversion Using toBinaryString() Method

In the example below, we are calling Integer.toBinaryString() method and int will return a binary value in String format.

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


Value in the binary system is: 10110111

Other than this inbuild method we go for implementation from scratch using long division.

Example: Int to Binary Conversion Using Long Division Method

This method is completely mathematical and here we declared an array of int 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 int. In the end, to get the result we traverse it in a reverse way.

public class StudyTonight 
{ 
	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]);
		}
	} 
} 


Value in binary system is:10110111

In the above method, we can see in a long division each time we are calculating reminder using mod operation and we are doing division also these operations are costly as compare to bit manipulation and we can do the same operations using bit-manipulation which is given below.

Example: Int to Binary Conversion Using Bit Manipulation

Using the right shift operator we can make any value to 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 StudyTonight 
{ 
	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());
	} 
} 


Value in binary system is:11101101

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

Integer.toString(int val, int radix)

Example: Int to Binary Conversion Using Integer.toString()

Integer.toString() is an inbuilt method that takes the first parameter as a value to which we want to convert and the second parameter as a radix which is also known as a base of a number system. In our case, we are interested in binary numbers so we are passing 2 and it will return a binary number of a given value in a string format.

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


Value in binary system is:10110111

Conclusion:

We can convert int to binary in java using two methods: The first method is very straight forward and we use toBinaryString() method from Integer class which will result in a string of a binary number that is equivalent to the number that is provided as an argument to this function. In the second method, we do a long division of a number by 2 each time and store carry this is a purely mathematical approach of converting int to binary number.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.