# Convert Decimal to Binary in Java

Sheeraz Gul Jan 30, 2023 May 27, 2022

There are different approaches to converting decimal to binary in Java. This tutorial demonstrates different ways how to convert decimal to binary in Java.

We can convert the decimal numbers to binary using the bitwise operators, arrays, and `Math.pow()` method in Java.

## Convert Decimal to Binary Using Bitwise Operators in Java

The bitwise operators usually work faster than the arithmetic operators; they can also be used to convert decimal to binary in Java. See example:

``````package delftstack;

import java.util.Scanner;

class Delftstack {
public void DecimalToBinary(int number) {
// Size of the integer is 32 bits
for (int x = number - 1; x >= 0; x--) {
int k = number >> x;
if ((k & 1) > 0) {
System.out.print("1");
}
else {
System.out.print("0");
}
}
}
}

public class Decimal_to_Binary {
public static void main(String[] args) {
Delftstack Demo = new Delftstack();
Scanner New_Number = new Scanner(System.in);
int number = New_Number.nextInt();
System.out.println("The Input Decimal Number : " + number);
System.out.print("The Output Binary Number : ");
Demo.DecimalToBinary(number);
}
}
``````

The code above will take an input of a decimal number and convert that number into binary. See the output:

``````Please enter a number
50
The Input Decimal Number : 50
The Output Binary Number : 00000000000011001000000000000000000000000000110010
``````

## Convert Decimal to Binary Using Arrays in Java

Follow the steps below to convert the decimal number to binary using arrays in Java.

• ##### Finally, print the array in the reverse order.

Let’s try an example based on the steps above.

``````package delftstack;
import java.io.*;
import java.util.Scanner;

class Decimal_to_Binary {
static void DecimalToBinary(int number) {
// an array which will store the binary number
int[] Binary_Number = new int;

int x = 0;
while (number > 0) {
// store the remainder in the array
Binary_Number[x] = number % 2;
number = number / 2;
x++;
}

// print the final array in reverse order
for (int y = x - 1; y >= 0; y--)
System.out.print(Binary_Number[y]);
}

public static void main (String[] args) {
Scanner New_Number = new Scanner(System.in);
int number = New_Number.nextInt();
System.out.println("The Input Decimal Number :" + number);
System.out.print("The Output Binary Number : ");
DecimalToBinary(number);
}
}
``````

The code above will take an input of a decimal number and convert that number into binary. See output:

``````Please enter a number
33
The Input Decimal Number :33
The Output Binary Number : 100001
``````

## Convert Decimal to Binary Using `Math.pow()` in Java

`Math.pow()` method can also convert the decimal number to binary. Let’s try an example.

``````package delftstack;
import java.io.*;
import java.util.Scanner;

public class Decimal_to_Binary {
static int DecimalToBinary(int number) {
int Binary_number = 0;
int count = 0;
while (number != 0) {
int remainder = number % 2;
double temp = Math.pow(10, count);
Binary_number += remainder * temp;
number /= 2;

count++;
}

return Binary_number;
}

public static void main(String[] args) {
Scanner New_Number = new Scanner(System.in);
int number = New_Number.nextInt();
System.out.println("The Input Decimal Number : " + number);
System.out.print("The Output Binary Number : ");
System.out.println(DecimalToBinary(number));
}
}
``````

The code above will take an input of a decimal number and convert that number into binary. See output:

``````Please enter a number
65
The Input Decimal Number : 65
The Output Binary Number : 1000001
``````
Author: Sheeraz Gul

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.