Java Program to Convert Decimal to Binary (Using Bitwise Operator, Array, Function, Recursion, and More)
Introduction
Converting a decimal number into its binary equivalent is a common task in programming, especially in low-level operations, competitive coding, and systems development.
In Java, this can be accomplished through a variety of techniques—from manual logic using loops to built-in methods.
This article explores different methods of decimal to binary conversion in Java with a focus on understanding the internal logic, bit manipulation, and recursive behavior.
Each method is explained step-by-step with a working program, sample output, and detailed code comments. These examples aim to help beginners and intermediate Java developers strengthen their understanding of number system conversions.
If you're searching for:
-
java convert decimal to binary
-
decimal to binary conversion java
-
or a program to convert decimal to binary using recursion, array, or bitwise operations
Then this guide is exactly what you need.
Problem Statement
Objective:
Write a Java program that takes a decimal (base-10) number as input and converts it to its binary (base-2) representation.
Use Cases
Here are practical areas where converting decimal to binary is commonly used:
-
Embedded Systems: Where binary values control hardware-level signals.
-
Computer Networks: IP address calculations and subnetting often use binary.
-
Bit Manipulation Tasks: Flags, masks, and bit-level programming require binary understanding.
-
Data Compression & Encryption: Operate on binary sequences for performance and security.
-
Academic & Competitive Programming: Often includes number system conversion challenges.
What You Will Learn
In this tutorial, you'll learn:
-
How to convert decimal to binary using:
-
Manual logic (modulus and division)
-
Bitwise operators
-
Arrays and recursion
-
Java built-in methods
-
-
How to use structured Java code with readable formatting
-
Step-by-step logic explanation for each approach
-
Sample output for better visualization
1. Java Program to Convert Decimal to Binary Using Bitwise Operator
// Java program to convert decimal to binary using bitwise operator
public class Main {
public static void main(String[] args) {
// Step-1 Declare and initialize the decimal number
int num = 10;
// Step-2 Check if the number is zero
if (num == 0) {
System.out.println("Binary representation: 0");
return;
}
// Step-3 Store result using bitwise operations
String binary = "";
for (int i = 31; i >= 0; i--) {
int bit = (num >> i) & 1;
binary += bit;
}
// Step-4 Trim leading zeros
binary = binary.replaceFirst("^0+(?!$)", "");
// Step-5 Output result
System.out.println("Binary representation: " + binary);
}
}
Output
Binary representation: 1010
Explanation of the Code:
-
Declare Decimal Number: We initialize a decimal number
num = 10
. -
Check for Zero: If the number is zero, we print
0
directly. -
Use Bitwise Operation: A loop runs from 31 to 0, shifting the number right by
i
bits and extracting each bit using bitwiseAND (& 1)
. -
Build Binary String: We build the binary string by appending bits from
MSB
toLSB
. -
Remove Leading Zeros: Regex is used to strip leading zeros from the binary result.
-
Output: Final binary string is printed.
2. Java Convert Decimal to Binary Using Integer Array
// Java program to convert decimal to binary using array
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Step-1 Declare variables
Scanner sc = new Scanner(System.in);
int[] binary = new int[32];
int index = 0;
// Step-2 Input decimal number
System.out.print("Enter a decimal number: ");
int num = sc.nextInt();
// Step-3 Convert to binary using division
while (num > 0) {
binary[index++] = num % 2;
num = num / 2;
}
// Step-4 Print binary in reverse order
System.out.print("Binary representation: ");
for (int i = index - 1; i >= 0; i--) {
System.out.print(binary[i]);
}
System.out.println();
sc.close();
}
}
Output
Enter a decimal number: 13
Binary representation: 1101
Explanation of the Code:
-
Declare Array: A fixed-size array is used to store binary digits.
-
Input Decimal Number: The user inputs a decimal number.
-
Convert to Binary: Using division and modulo (
% 2
), we store the remainder in the array. -
Reverse Output: Since binary is built in reverse, we print from the last index to the first.
-
Close Scanner: Free the resource after use.
3. Java Program to Convert Decimal to Binary Using Function
// Java program to convert decimal to binary using a function
import java.util.Scanner;
public class Main {
// Function to convert decimal to binary and return result
static String convertToBinary(int num) {
String binary = "";
while (num > 0) {
binary = (num % 2) + binary;
num = num / 2;
}
return binary.equals("") ? "0" : binary;
}
public static void main(String[] args) {
// Step-1 Input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int num = sc.nextInt();
// Step-2 Convert using function
String binary = convertToBinary(num);
// Step-3 Output result
System.out.println("Binary representation: " + binary);
sc.close();
}
}
Output
Enter a decimal number: 7
Binary representation: 111
Explanation of the Code:
-
Create Function:
convertToBinary(int num)
is a utility function that handles the logic. -
Inside Function:
-
We use a
while
loop to build the binary string fromLSB
toMSB
. -
Each digit is prepended to the string.
-
-
Main Method:
-
Takes input, calls the function, and prints the result.
-
4. Java Program to Convert Decimal to Binary Using Recursion
// Java program to convert decimal to binary using recursion
import java.util.Scanner;
public class Main {
// Recursive function to print binary
static void convertToBinary(int num) {
if (num == 0)
return;
convertToBinary(num / 2);
System.out.print(num % 2);
}
public static void main(String[] args) {
// Step-1 Input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int num = sc.nextInt();
// Step-2 Handle zero case
System.out.print("Binary representation: ");
if (num == 0)
System.out.print("0");
else
convertToBinary(num);
System.out.println();
sc.close();
}
}
Output
Enter a decimal number: 6
Binary representation: 110
Explanation of the Code:
-
Create Recursive Function:
convertToBinary(int num)
prints digits recursively. -
Recursive Logic:
-
It divides the number by 2 and calls itself until the base case (0).
-
After returning, it prints
num % 2
.
-
-
Main Method:
-
Takes input and calls the function.
-
Handles zero as a special case.
-
5. Java Convert Decimal to Binary Using Built-in Method
// Java program using built-in Integer.toBinaryString()
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// Step-1 Input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a decimal number: ");
int num = sc.nextInt();
// Step-2 Use built-in method
String binary = Integer.toBinaryString(num);
// Step-3 Output result
System.out.println("Binary representation: " + binary);
sc.close();
}
}
Output
Enter a decimal number: 14
Binary representation: 1110
Explanation of the Code:
-
Input: Read a decimal number using
Scanner
. -
Conversion:
Integer.toBinaryString(num)
converts the number directly. -
Output: Print the returned binary string.
Final Thoughts
Each of these methods provides a different perspective on how decimal to binary conversion in Java can be implemented. From bitwise logic to recursion and built-in utilities, every method helps build deeper knowledge about binary systems and Java programming fundamentals.