
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Binary Number to Decimal Number in Java
In this article, we will learn to convert a binary number into a decimal number in Java.Binary numbers are fundamental in computer science and digital electronics, where data is represented in a base-2 numeral system consisting of only 0s and 1s. Converting binary numbers to their decimal equivalents is a common task.
Problem Statement
The task is to convert a binary number into its decimal equivalent using Java. ?
Input
1110
Output
14
Different Approaches
Following are the two different approaches to converting a binary number into a decimal number in Java ?
Using Integer.parseInt()
Java's Integer class includes a built-in method, parseInt(), which can directly convert a binary string into its decimal equivalent. The method takes two arguments, the string representation of the binary number and the base (radix) of the number system.
Following are the steps to convert binary numbers to decimal numbers using parseInt() ?
- First, we will create a class called Demo.
- Inside the main method, we will call the parseInt() method of the Integer class.
- The binary string "1110" will be passed as the first argument, and the radix 2 as the second argument.
- Finally, we will print the result, which is the decimal equivalent of the binary number.
Integer.parseInt("1110", 2)
Example
Below is the Java program to convert binary numbers to decimal numbers using parseInt() ?
public class Demo { public static void main( String args[] ) { // converting to decimal System.out.println(Integer.parseInt("1110", 2)); } }
Output
14
Time Complexity: O(n), where n is the length of the binary string.
Space Complexity: O(1), constant extra space.
Using Manual Conversion
If you prefer to understand how the conversion works behind the scenes or if you want to write your own logic for converting binary to decimal, you can manually calculate the decimal value using the positional value of each binary digit.
Following are the steps to convert binary numbers to decimal numbers using manual conversion ?
- Start from the least significant bit (rightmost digit) and move to the most significant bit (leftmost digit).
- Multiply each bit by 2^i, where i is the position of the bit (starting from 0).
- Sum up all the values to get the decimal equivalent.
for (int i = 0; i < binary.length(); i++) { int bit = Character.getNumericValue(binary.charAt(binary.length() - 1 - i)); decimal += bit * Math.pow(2, i); }
Example
Below is the Java program to convert binary numbers to decimal numbers using manual conversion ?
public class ManualBinaryToDecimal { public static void main(String[] args) { String binary = "1110"; int decimal = 0; for (int i = 0; i < binary.length(); i++) { int bit = Character.getNumericValue(binary.charAt(binary.length() - 1 - i)); decimal += bit * Math.pow(2, i); } System.out.println(decimal); } }
Output
14
Time Complexity: O(n), where n is the length of the binary string.
Space Complexity: O(1), constant extra space.
Comparison Table
Feature | Using Integer.parseInt() | Manual Conversion |
Ease of Use | Very simple and concise | Slightly more complex |
Performance | Fast, optimized inbuilt method | Slower for large binary numbers |
Educational Value | Limited | Demonstrates the underlying logic |