Problem: Write a Java program to convert a binary number into the decimal format, without using any library method which can directly solve the problem. You are free to use basic Java functions through e.g. those defined in java.lang and all kinds of Java operators e.g. arithmetic and logical operator, bitwise and bitshift operator, and relational operators.
Solution: Let's first revise some theory of the number system, which is required to convert a number from binary to decimal format. There are four kinds 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. A Decimal system is what we humans use, it uses 10 digits to represent any number from 0 to 9.
Solution: Let's first revise some theory of the number system, which is required to convert a number from binary to decimal format. There are four kinds 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. A Decimal system is what we humans use, it uses 10 digits to represent any number from 0 to 9.
The hexadecimal number is base 16 and uses 16 digits to represent a number. Binary is what computers and electronic devices use and Decimal is what we humans use.
If you remember the algorithm for converting a binary number to decimal in college, you would know that we multiply bits in respective positions with 2 to the power of their position, which is zero-based. We will use the same algorithm here to convert a binary number into a decimal. The only difference is that now we will implement this algorithm in Java.
One more thing to remember is that in order to represent the same number you would need more digits in the lower base. For example, to represent 8 in binary you need three bits 111, while it only requires one digit 8 to represent the same number in decimal format.
By the way, this is the second part of the binary to decimal conversion tutorial, in the 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.
The algorithm works by getting the 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 the modulus operator e.g. number%10 will give you the last digit.
If you remember the algorithm for converting a binary number to decimal in college, you would know that we multiply bits in respective positions with 2 to the power of their position, which is zero-based. We will use the same algorithm here to convert a binary number into a decimal. The only difference is that now we will implement this algorithm in Java.
One more thing to remember is that in order to represent the same number you would need more digits in the lower base. For example, to represent 8 in binary you need three bits 111, while it only requires one digit 8 to represent the same number in decimal format.
By the way, this is the second part of the binary to decimal conversion tutorial, in the 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 - Example
Here is our sample program to convert a given binary integer into decimal format. The binary number is passed as an 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 does to represent negative integers in binary.The algorithm works by getting the 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 the 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 the input is 101 then it will run 3 times, if the input is 1001 then it will four times.
So it's complexity is O(n) because it will need n iteration to convert an N digit binary number into decimal. This flowchart will also help you to understand binary to decimal conversion algorithms better
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 the Java library method like Integer.toBinaryString() and you should use it instead of writing your own method.
Related Data Structure and Algorithm Interview Questions from Javarevisited Blog
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you.
/** * * 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 the Java library method like Integer.toBinaryString() and you should use it instead of writing your own method.
The reason is, the API methods are well tested and tried by thousands of developers, so they will less likely have any bugs than your method. Here is an example of using Java API to convert decimal numbers to binary in Java.
Related Data Structure and Algorithm Interview Questions from Javarevisited Blog
- Difference between array and linked list data structure? (answer)
- Difference between a binary tree and a 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)
- 21 String coding problem for interview (string questions)
- 20+ Array-based coding problems for interviews (array questions)
- 40 Binary Tree-based coding problems for interviews (binary tree questions)
- 25 software design questions for interview (design questions)
Thanks for reading this article so far. If you like this article then please share it with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you.
P. S. - If you are looking for some Free Data Structures and Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check these best data structure courses in Java. It contains the best courses Java programmers can take to build their DS and Algo skills.
And lastly one question for you? Which one is your favorite coding problem in Java? Palindrome, Prime number, Fibonacci, Factorial or this one?
For better performance, Math.pow(2, power) should be replaced by (1 << power):
ReplyDeletedecimal += lastDigit * (1 << power);
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.
ReplyDeletee.g.
public static int binaryToDecimal(int bin) {
return (bin<2) ? bin : binaryToDecimal(bin / 10) * 2 + (bin % 10);
}
How would u do signed binary to decimal
ReplyDelete