Java
Programming
Binary Conversion
Coding Tutorial
Integer Operation

Print an integer in binary format in Java

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Printing an integer in binary in Java is easy if all you need is the basic representation. The details matter when you care about fixed width, leading zeros, or negative numbers. The best method depends on whether you want a quick debug string or a controlled bit-level format.

The Simplest Method: Integer.toBinaryString

For most cases, use the standard library.

java
int number = 10;
String binary = Integer.toBinaryString(number);
System.out.println(binary);

That prints:

text
1010

This is usually the right choice for logging, debugging, and quick conversions.

Understand What Happens with Negative Numbers

Integer.toBinaryString treats the value as an unsigned 32-bit two's-complement bit pattern.

java
int number = -10;
System.out.println(Integer.toBinaryString(number));

That will print the full 32-bit two's-complement representation, not a simple string like -1010.

This is correct for bit-level debugging, but it surprises people who expect a minus sign plus the positive binary digits.

Pad with Leading Zeros When Width Matters

If you need a fixed-width binary string, pad the standard output manually.

java
1int number = 10;
2String binary = Integer.toBinaryString(number);
3String padded = String.format("%8s", binary).replace(' ', '0');
4
5System.out.println(padded);

Output:

text
00001010

This is useful for bytes, protocol fields, and UI displays where all values should have the same width.

Use Bit Operations for Full Control

If you want exact control over how many bits are shown, a manual loop is often clearer than string formatting tricks.

java
1public static String toBinary(int value, int bits) {
2    StringBuilder builder = new StringBuilder(bits);
3
4    for (int i = bits - 1; i >= 0; i--) {
5        int mask = 1 << i;
6        builder.append((value & mask) != 0 ? '1' : '0');
7    }
8
9    return builder.toString();
10}
11
12public static void main(String[] args) {
13    System.out.println(toBinary(10, 8));
14}

This is especially good when you want exactly 8, 16, or 32 bits and do not want to rely on padding after the fact.

Use the Right Type Helper

Java provides matching helpers for other integer sizes:

  • 'Integer.toBinaryString(int)'
  • 'Long.toBinaryString(long)'

So if you are working with long, do not force it through Integer APIs.

java
long value = 42L;
System.out.println(Long.toBinaryString(value));

Choosing the correct helper avoids accidental truncation.

Decide Whether You Want Numeric Meaning or Bit Pattern Meaning

This is the conceptual question behind most confusion:

  • do you want the raw bit pattern?
  • or do you want a human-readable signed representation?

For raw bit patterns, Integer.toBinaryString and fixed-width bit loops are correct.

For human-readable formatting of signed numbers, you may need to handle the sign separately:

java
1int value = -10;
2String text = value < 0
3    ? "-" + Integer.toBinaryString(-value)
4    : Integer.toBinaryString(value);
5
6System.out.println(text);

That is a different output convention from Java's native two's-complement view.

Common Pitfalls

  • Assuming Integer.toBinaryString(-10) will print -1010.
  • Forgetting to pad the result when fixed width matters.
  • Using Integer helpers for values that should be handled as long.
  • Confusing a signed textual form with the raw two's-complement bit pattern.
  • Writing manual bit loops when the standard library already solves the simple case.

Summary

  • Use Integer.toBinaryString for the simplest Java binary output.
  • Pad with zeros if you need a fixed width.
  • Use a manual bit loop when you need exact control over the displayed bit count.
  • Negative numbers are shown as two's-complement bit patterns by the standard helper.
  • Decide whether you want a raw bit representation or a human-readable signed representation before choosing the formatting approach.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.