Java Integer toBinaryString() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Integer

Definition

In Java, Integer.toBinaryString(n) returns the bits of n as a String of 0 and 1 characters. It treats the value as an unsigned 32-bit number and leaves out leading zeros.

Positive numbers give short strings, such as "101" for 5. Negative numbers always give all 32 bits, because the top bit is set.

A static method of java.lang.Integer: call it on the class, as Integer.toBinaryString(...). Integer lives in java.lang, so no import is needed.

Syntax

One argument: the int to show.

Java
String a = Integer.toBinaryString(5);     // "101"
String b = Integer.toBinaryString(0);     // "0"
String c = Integer.toBinaryString(-1);    // "11111111111111111111111111111111"
CallWhat it does
Integer.toBinaryString(n)Unsigned binary text of n, with no leading zeros.
Integer.toString(n, 2)Signed text: a minus sign plus the binary digits of the positive twin.
String.format("%8s", bits).replace(' ', '0')Pads the text with zeros on the left to a fixed width.
Integer.parseInt(bits, 2)The reverse: reads binary text back into an int.

The result has no 0b prefix. For a negative number the length is always 32; for others it is as short as possible.

Library declarations
Java
// java.lang.Integer
public static String toBinaryString(int i)

Parameters

ParameterDescription
i (int)The number whose bits you want. Negative values are read as unsigned 32-bit numbers.

Return Value

Return ValueDescription
StringThe binary digits, 1 to 32 characters long, without a sign and without leading zeros (except for the single "0").

Compare with Similar Methods

Related conversions:

MethodHow it differs
toString(n, 2)Signed binary text, such as "-101" for -5.
toHexString(n)The same raw bits, written more compactly in base 16.
toOctalString(n)The same raw bits in base 8.
bitCount(n)Counts the 1 characters without building a string.

Java Examples

Example 1 — Show Small Numbers in Binary

Print several numbers next to their binary text, with and without the zero padding to 8 bits.

Java
public class IntegerToBinaryDemo {
    static String pad8(int n) {
        return String.format("%8s", Integer.toBinaryString(n)).replace(' ', '0');
    }

    public static void main(String[] args) {
        int[] numbers = {0, 5, 65, 200, 255, 1024};
        for (int n : numbers) {
            System.out.println(n + " -> " + Integer.toBinaryString(n) + " -> " + pad8(n));
        }
    }
}

How It Works

  1. No leading zeros. 5 becomes 101 and 1024 becomes a 1 followed by ten zeros.
  2. Zero is one digit. The number 0 gives the single character "0", not an empty string.
  3. Padding is manual. %8s right-aligns the text in 8 characters with spaces, and replace turns those spaces into zeros.
  4. Longer than 8 bits. 1024 needs 11 bits, so padding to 8 changes nothing.

Example 2 — See the 32 Bits of Negative Numbers

Print negative numbers in binary and compare them with their positive twins.

Java
public class IntegerToBinaryNegative {
    public static void main(String[] args) {
        int[] numbers = {1, -1, 8, -8};
        for (int n : numbers) {
            String bits = Integer.toBinaryString(n);
            System.out.println(n + " (length " + bits.length() + "): " + bits);
        }
        System.out.println("MIN_VALUE: " + Integer.toBinaryString(Integer.MIN_VALUE));
        System.out.println("signed -5:  " + Integer.toString(-5, 2));
    }
}

How It Works

  1. Positive numbers are short. 1 is 1 and 8 is 1000, with no leading zeros to waste space.
  2. Negative numbers fill all 32 bits. The top bit is 1 for any negative number, so the string is always 32 characters long.
  3. -1 is all ones. In two's complement, flipping every bit of 0 gives -1. -8 is the bits of 8 flipped, plus one, which ends in 000.
  4. The smallest int. MIN_VALUE is a 1 followed by 31 zeros, while toString(-5, 2) only writes a minus sign and 101.

Example 3 — Show and Test Permission Flags as Bits

Combine permission flags with | and print the bit pattern to see which flags are on.

Java
public class IntegerToBinaryFlags {
    static final int READ = 1;    // 001
    static final int WRITE = 2;   // 010
    static final int EXEC = 4;    // 100

    static String bits3(int n) {
        return String.format("%3s", Integer.toBinaryString(n)).replace(' ', '0');
    }

    public static void main(String[] args) {
        int permissions = READ | EXEC;
        System.out.println("permissions:    " + bits3(permissions));
        System.out.println("can write:      " + ((permissions & WRITE) != 0));

        permissions |= WRITE;
        System.out.println("after adding:   " + bits3(permissions));

        permissions &= ~READ;
        System.out.println("after removing: " + bits3(permissions));
    }
}

How It Works

  1. One bit per flag. READ, WRITE, and EXEC use the bits 001, 010, and 100, so they never overlap.
  2. OR combines. READ | EXEC sets the first and third bits, which prints as 101.
  3. AND tests. permissions & WRITE is zero because the middle bit is off, so the answer is false.
  4. Add and remove. |= WRITE turns the middle bit on to give 111, and &= ~READ clears the lowest bit to give 110.

Frequently Asked Questions

Call Integer.toBinaryString(n). For example, Integer.toBinaryString(5) returns "101".
Pad it yourself, for example String.format("%8s", Integer.toBinaryString(n)).replace(' ', '0') for 8 bits.
toBinaryString shows the unsigned 32-bit pattern. A negative int always has its top bit set, so there are no leading zeros to drop.
toBinaryString shows raw bits and never uses a minus sign. toString(n, 2) writes a minus sign followed by the binary digits of the positive number.
Use Integer.parseInt(text, 2). For a full 32-bit pattern starting with 1, use Integer.parseUnsignedInt(text, 2).

Next: toHexString()

Show the bits of an int in hexadecimal.

toHexString() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful