Integer.toBinaryString() method in Java

The toBinaryString() method is a static method of the Integer wrapper class that is used to convert the integer into its corresponding binary string. In this post, we are going to look at the toBinaryString() method in detail.

  • Method declaration – public static String toBinaryString(int i)
  • What does it do? It will take an integer as an argument and then will convert it into a binary string by processing the input as an unsigned integer
  • What does it return? It will return a binary string

Now, what are signed and unsigned integers? Signed integers contain both positive and negative numbers and their range is from [-2147483648 to 2147483647] whereas unsigned integers contain only the non-negative numbers ranging from [0 to 4294967295].

We will see how the toBinaryString() method will convert positive and negative numbers into their corresponding binary strings.

Using toBinaryString() method on a positive integer

Positive numbers can easily be represented using binary strings without taking the 2’s complement of the number. So, 30 would be represented as 11110 in the binary form.

public class Codekru {

	public static void main(String[] args) {

		int i = 30;

		System.out.println("Binary representation of " + i + " is: " + Integer.toBinaryString(i));
	}
}

Output –

Binary representation of 30 is: 11110
Using the toBinaryString() method on a negative integer

Negative numbers cannot be directly represented by the binary string. We have to find the 2’s complement of the negative numbers to represent them using the binary string. So, let’s first understand how we can find the 2’s complement of a negative number.

  • To find the 2’s complement of a number, we first have to know how many bits we are working with. In our case, the number of bits is 32
  • Let’s try to find the 2’s complement of the number -30
  • For this, we first have to write the binary representation of the number 30
0000 0000 0000 0000 0000 0000 0001 1110
  • Invert the binary digits
1111 1111 1111 1111 1111 1111 1110 0001
  • Now, add one to it
1111 1111 1111 1111 1111 1111 1110 0010

public class Codekru {

	public static void main(String[] args) {

		int i = -30;

		System.out.println("Binary representation of " + i + " is: " + Integer.toBinaryString(i));
	}
}

Output –

Binary representation of -30 is: 11111111111111111111111111100010

But now the question is how toBinaryString() method is finding the binary string of a negative integer?

Negative integers are first converted to unsigned integers by adding 232(4294967296) to them. As we are trying to find the binary representation of -30, so, it would be converted to an unsigned integer by doing -30 + 232, and then the resulting 4294967266 would be used to represent -30 as the binary representation.

  • The binary representation of 4294967266 = 11111111111111111111111111100010
  • The binary representation of -30 = 11111111111111111111111111100010
How can we recover the original integer from the binary string?

We can easily recover the integer from the binary string by using the parseUnsignedInt() method with the base as 2. The below program might help in clarifying things a bit better.

public class Codekru {

	public static void main(String[] args) {

		int i = 30;

		String binaryString = Integer.toBinaryString(i);

		System.out.println("Binary representation of " + i + " is: " + binaryString);

		int originalInteger = Integer.parseUnsignedInt(binaryString, 2);

		System.out.println("Original integer: " + originalInteger);

	}
}

Output –

Binary representation of 30 is: 11110
Original integer: 30

We can restore the negative integers also by using the same method.

Please visit this link if you want to know more about the Integer wrapper class of java and its other functions or methods.

Hope you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Related Articles
Liked the article? Share this on

Leave a Comment

Your email address will not be published.