Quantcast
Channel: Baeldung
Viewing all articles
Browse latest Browse all 4535

Print an Integer in Binary Format in Java

$
0
0

1. Overview

In this tutorial, we'll look briefly at the different ways of printing an integer in binary format in Java.

First, we'll take a conceptual look. And then, we'll learn some built-in Java functions for conversion.

2. Using Integer to Binary Conversion

In this section, we'll write our custom method to convert an integer into a binary format string in Java. Before writing the code, let's first understand how to convert an integer into a binary format.

To convert an integer n into its binary format, we need to:

  1. Store the remainder when number n is divided by 2 and update the number n with the value of the quotient
  2. Repeat step 1 until the number n is greater than zero
  3. Finally, print the remainders in reverse order

Let's see an example of converting 7 into its binary format equivalent:

  1. First, divide 7 by 2: remainder 1, quotient 3
  2. Second, divide 3 by 2: remainder 1, quotient 1
  3. Then, divide 1 by 2: remainder 1, quotient 0
  4. And finally, print the remainders in reverse order since the quotient in the previous step is 0: 111

Next, let's implement the above algorithm:

public static String convertIntegerToBinary(int n) {
    if (n == 0) {
        return "0";
    }
    StringBuilder binaryNumber = new StringBuilder();
    while (n > 0) {
        int remainder = n % 2;
        binaryNumber.append(remainder);
        n /= 2;
    }
    binaryNumber = binaryNumber.reverse();
    return binaryNumber.toString();
}

3. Using Integer#toBinaryString Method

Java's Integer class has a method named toBinaryString to convert an integer into its binary equivalent string.

Let's look at the signature of the Integer#toBinaryString method:

public static String toBinaryString(int i)

It takes an integer argument and returns a binary string representation of that integer:

int n = 7;
String binaryString = Integer.toBinaryString(n);
assertEquals("111", binaryString);

4. Using Integer#toString Method

Now, let's look at the signature of the Integer#toString method:

public static String toString(int i, int radix)

The Integer#toString method is an in-built method in Java that takes two arguments. First, it takes an integer that is to be converted to a string. Second, it takes radix that is to be used while converting the integer into its string representation.

It returns a string representation of the integer input in the base specified by the radix.

Let's use this method to convert an integer into its binary format using a radix value of 2:

int n = 7;
String binaryString = Integer.toString(n, 2);
assertEquals("111", binaryString);

As we can see that we passed the radix value of 2 while calling the Integer#toString method to convert the integer n into its binary string representation.

5. Conclusion

In conclusion, we looked at integer to binary conversion. Furthermore, we saw a couple of built-in Java methods to convert an integer into a string in binary format.

As always, all these code samples are available over on GitHub.


Viewing all articles
Browse latest Browse all 4535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>