Integer to Binary

In This Article we will see how we can convert Integer to Binary number. We will see some built in options that Java provides as well as two different custom implementation.
Using Integer.toBinaryString
Using Integer.toBinaryString() method we can convert a integer to Binary String very easily
private static String toBinary1(Integer i) {
    return Integer.toBinaryString(i);
}
Using Integer.toString()
We can also use Integer.toString() and pass the number and radix to convert the integer to String. In this case we pass 2 as radix to convert the Integer to Binary Number String.
 private static String toBinary2(Integer i) {
    return Integer.toString(i, 2);
  }
Using BigInteger
BigInteger also provides a toString() method where we can pass 2 as radix to convert it to Binary Number String.
private static String toBinary2(Integer i) {
    BigInteger bigInt = new BigInteger(String.valueOf(i));
    return bigInt.toString(2);
  }
By Dividing.
Following code is a custom method to convert an Integer to Binary.
private static String toBinary_divide(Integer i) {

    StringBuilder ret = new StringBuilder();

    while (i > 0) {
      
      if (i % 2 == 1) {
        ret.insert(0, "1");
      }
      else {
        ret.insert(0, "0");
      }
      i = i / 2;
    } 

    return ret.toString();
  }
By using Bit masking
This is another custom method to convert Integer to Binary String. This Algorithm works by masking each bit in the original number and creating a char array. And finally converting the char array to String.
private static String toBinary_mask(Integer num) {
    char ret[] = new char[32];

    for (int i = 0; i < 32; i++) {
      int mask = 1 << i;
      ret[31 - i] = (num & mask) != 0 ? '1' : '0';
    }
    return new String(ret);
  }

Full Code

IntegerToBinaryConverter.java
import java.math.BigInteger;

public class IntegerToBinaryConverter {

  public static void main(String[] args) {
    System.out.println("Convert to Binary 2020 = " + toBinary1(2020));
    System.out.println("Convert to Binary 2020 = " + toBinary2(2020));
    System.out.println("Convert to Binary 2020 = " + toBinary3(2020));
    System.out.println("Convert to Binary 2020 = " + toBinary_divide(2020));
    System.out.println("Convert to Binary 2020 = " + toBinary_mask(2020));
  }

  private static String toBinary1(Integer i) {
    return Integer.toBinaryString(i);
  }

  private static String toBinary2(Integer i) {
    return Integer.toString(i, 2);
  }

  private static String toBinary3(Integer i) {
    BigInteger bigInt = new BigInteger(String.valueOf(i));
    return bigInt.toString(2);
  }

  private static String toBinary_divide(Integer i) {
    StringBuilder ret = new StringBuilder();
    while (i > 0) {
      if (i % 2 == 1) {
        ret.insert(0, "1");
      }
      else {
        ret.insert(0, "0");
      }
      i = i / 2;
    } 
    return ret.toString();
  }

  private static String toBinary_mask(Integer num) {
    char ret[] = new char[32];
    for (int i = 0; i < 32; i++) {
      int mask = 1 << i;
      ret[31 - i] = (num & mask) != 0 ? '1' : '0';
    }
    return new String(ret);
  }
}
Console Output
Convert to Binary 2020 = 11111100100 
Convert to Binary 2020 = 11111100100 
Convert to Binary 2020 = 11111100100 
Convert to Binary 2020 = 11111100100 
Convert to Binary 2020 = 00000000000000000000011111100100

Summary

This article shows different ways to convert the Integer to Binary String. The Bit masking is very efficient and in fact the builtin methods like Integer.toString() uses Bit masking internally. For more information on Bit masking the links provided in Reference section is very useful.

References