HOWTO · Java

How to Convert Int to Binary in Java

This article explains the methods to convert an int to binary in Java

On this page

To convert an int to a binary String in Java, use Integer.toBinaryString(value) for the usual representation, Integer.toString(value, 2) when you want to specify the radix, or a StringBuilder loop when you need to illustrate the conversion algorithm. The sections below explain which representation each approach returns and where the manual loop applies.

Convert Int to Binary Using Integer.toBinaryString() in Java

Integer.toBinaryString() accepts an int and returns its binary digits as a String. For the value 150, the result has no leading zeroes and is represented as 10010110. This is a concise choice when you need the binary representation of an integer.

Run this example:

public class JavaExample {
  public static void main(String[] args) {
    int value = 150;
    String binary = Integer.toBinaryString(value);
    System.out.println(binary);
  }
}
10010110

For a negative int, this method returns the 32-bit two’s-complement representation, including leading 1 bits as needed. That fixed-width result differs from a signed binary numeral with a minus sign.

Convert Int to Binary Using Integer.toString() in Java

Integer.toString(value, radix) converts the value to a string using the given radix. Pass 2 for binary. For positive integers, it produces the same unpadded digit form as Integer.toBinaryString(). This overload is useful when the radix is a parameter in a larger conversion routine.

Run this example:

public class JavaExample {
  public static void main(String[] args) {
    int value = 200;
    String binary = Integer.toString(value, 2);
    System.out.println(binary);
  }
}
11001000

For negative values, Integer.toString(value, 2) puts a minus sign before the digits of the magnitude. Choose this method when that signed representation is what the caller needs. Both library methods accept Java’s int range; neither adds a chosen display width or leading zeroes for positive values.

Convert Int to Binary Using StringBuilder and a Loop in Java

A manual conversion can show how each digit is formed. This implementation accepts nonnegative values only. It handles zero separately because a loop that runs while the value is greater than zero would otherwise append no digits. For every other input, dividing by two repeatedly gives remainders of 0 or 1, from the least significant digit toward the most significant one. The collected digits therefore need reversing before they are returned.

Run this example with a nonnegative value:

public class JavaExample {
  static String convertIntToBinary(int value) {
    if (value < 0) {
      throw new IllegalArgumentException("value must be nonnegative");
    }
    if (value == 0) {
      return "0";
    }

    StringBuilder digits = new StringBuilder();
    while (value > 0) {
      digits.append(value % 2);
      value /= 2;
    }
    return digits.reverse().toString();
  }

  public static void main(String[] args) {
    System.out.println(convertIntToBinary(150));
  }
}
10010110

The manual method is useful for learning the repeated-division process or adapting it for a specific representation. Its explicit guard prevents negative input from silently producing an empty string. For general signed conversion, prefer one of the library methods and choose it according to the negative-value representation required.