# How to Convert Binary Number to Decimal in Java - Algorithm

Problem : Write a Java program to convert a binary number into decimal format, without using any library method which can directly solve the problem. You are free to use basic Java functions though e.g. those defined in java.lang and all kinds of Java operator e.g. arithmetic and logical operator, bitwise and bitshift operator and relational operators.

Solution : Let's first revise some theory of number system, which is required to convert a number from binary to decimal format. There are four kind of number systems binary, octal, decimal and hexadecimal. Binary is base 2 and that's why any number is represented using only two digit, 0 and 1 also known as bits.

The octal system is base 8 and you can use 8 digits to represent any number, from 0 to 7. Decimal system is what we human use, it uses 10 digits to represent any number from 0 to 9. Hexadecimal number is base 16 and uses 16 digit to represent a number. Binary is what computer and electronic devices use and Decimal is what we human use.

If you remember the algorithm for converting a binary number to decimal in college, you would know that we multiply bits in respective position with 2 to the power of there position, which is zero based. We will use the same algorithm here to convert a binary number into decimal. Only difference is that now we will implement this algorithm in Java.

One more thing to remember is that, in order to represent same number you would need more digits in lower base. For example, to represent 8 in binary you need three bits 111, while it only require one digit 8 to represent same number in decimal format.

Also, basic knowledge of essential data structure is also very important and that's why I suggest all Java programmers join a comprehensive Data Structure and Algorithms course like Data Structures and Algorithms: Deep Dive Using Java on Udemy to fill the gaps in your understanding.

By the way this is the second part of binary to decimal conversion tutorial, in first part we have already seen how to convert a decimal number to binary, so if you have not read it already, check it out.

## Algorithm to convert Binary to Decimal in Java

Here is our sample program to convert a given binary integer into decimal format.  Binary number is passed as integer but we only consider its digits and not actual value. So input will always use 0 and 1. We are also not considering them as 2's complement number as Java do to represent negative integers in binary.

The algorithm works by getting last digit in each iteration and then multiply it by 2^position, where position starts from zero. You can get the last digit of a number by using modulus operator e.g. number%10 will give you the last digit.

The rightmost bit is known as first position and should be multiplied by 2 to the power zero i.e. 1. The loop continues till all digits are processed i.e. if input is 101 then it will run 3 times, if input is 1001 then it will four times.

So it's complexity is O(n) because it will need n iteration to convert a n digit binary number into decimal. This flowchart will also help you to understand binary to decimal conversion algorithm better

```/**
*
* Write a Java program to convert Binary number to Decimal format.
*
* @author Javin Paul
*/
public class BinaryToDecimal {

public static void main(String args[]) {

System.out.printf("Decimal equivalent of binary number %d is %d %n",
101, binaryToDecimal(101));
System.out.printf("%d in binary format is %d %n",
111, binaryToDecimal(111));
System.out.printf("Decimal equivalent of binary number %d is %d %n",
10111, binaryToDecimal(10111));
System.out.printf("Decimal equivalent of binary number %d is %d %n",
1011, binaryToDecimal(1011));

}

/*
* Java algorithm to convert binary to decimal format
*/
public static int binaryToDecimal(int number) {
int decimal = 0;
int binary = number;
int power = 0;

while (binary != 0) {
int lastDigit = binary % 10;
decimal += lastDigit * Math.pow(2, power);
power++;
binary = binary / 10;
}
return decimal;
}
}

Output
Decimal equivalent of binary number 101 is 5
111 in binary format is 7
Decimal equivalent of binary number 10111 is 23
Decimal equivalent of binary number 1011 is 11```

That's all about how do you convert a binary number into decimal in Java. The key here is you cannot use Java API and you have come up with an algorithm to do this conversion. By the way in production code you can always solve this problem easily by using Java library method e.g. Integer.toBinaryString() and you should use it instead of writing your own method. Reason is, the API methods are well tested and tried by thousands of developers, so they will less likely have any bug then your method. Here is an example of using Java API to convert decimal number to binary in Java

Further Learning
Data Structures and Algorithms: Deep Dive Using Java
Algorithms and Data Structures - Part 1 and 2
Data Structures in Java 9 by Heinz Kabutz

Related Data Structure and Algorithm Interview Questions from Javarevisited Blog
• Difference between a binary tree and binary search tree? (answer)
• How to reverse a linked list in Java using iteration and recursion? (solution)
• How to reverse an array in place in Java? (solution)
• How to find all permutations of a String in Java? (solution)
• How to reverse a String in place in Java? (solution)
• How to remove duplicate elements from an array without using Collections? (solution)
• Top 5 Books on Data Structure and Algorithms for Java Developers (books)
• Top 5 books on Programming/Coding Interviews (list)

Jean-François Morin said...

For better performance, Math.pow(2, power) should be replaced by (1 << power):

decimal += lastDigit * (1 << power);

Baavgai said...

I'd only use Math.pow as a last resort. If you can reverse the input, you can just double as you read the bits.

e.g.
public static int binaryToDecimal(int bin) {
return (bin<2) ? bin : binaryToDecimal(bin / 10) * 2 + (bin % 10);
}

Devan said...

How would u do signed binary to decimal