Binary to Decimal Conversion in Java
Last Updated :
04 Mar, 2024
You are given a binary number as input and your task is to convert that number to its decimal equivalent through a Java program.
Examples :
Input : 1100
Output : 12
Input : 1111
Output : 15
Methods for Binary to Decimal Conversions
There are certain methods used for Binary to Decimal Conversions mentioned below:
- Basic Approach
- Using pre-defined function
- Using Bitwise operators
1. Basic Approach for Binary to Decimal Conversion
So the basic idea for converting a binary number to its decimal equivalent is to multiply each digit in the binary number by 2 raised to the power of its positional value and then add up these values.
For example :
Input: 1100
= 1*2^3 + 1*2^2 + 0*2^1 + 0*2^0
= 8 + 4 + 0 + 0
Output: 12
Below is a Java Code for the above-mentioned approach (Works both for String as well as integer Data Type ):
We have used here switch case for different input types( i.e. String or integer ). Users have to enter their choice of input. The given choice will execute/call their respective function.
Below is the implementation of the above method:
Java
import java.util.*;
class GFG {
public static void StringBinaryToDecimal(String s)
{
int ans = 0 , i, p = 0 ;
int len = s.length();
for (i = len - 1 ; i >= 0 ; i--) {
if (s.charAt(i) == '1' ) {
ans += Math.pow( 2 , p);
}
p++;
}
System.out.println("Decimal Equivalent of " + s
+ " is " + ans);
}
public static void IntegerBinaryToDecimal( int num)
{
int ans = 0 , rem = 0 , temp = num;
int i = 0 ;
while (num != 0 ) {
rem = num % 10 ;
num /= 10 ;
ans += rem * Math.pow( 2 , i);
i++;
}
System.out.println("Decimal Equivalent of " + temp
+ " is " + ans);
}
public static void main(String[] args)
{
int num = 1010 ;
String s = " 1100 ";
IntegerBinaryToDecimal(num);
StringBinaryToDecimal(s);
}
}
|
Output
Decimal Equivalent of 1010 is = 10
Decimal Equivalent of 1100 is = 12
Complexity of the above method:
Time complexity : O(log n)
Auxiliary Space : O(1)
Here n is total number of digits in the given binary number.
If you are dealing with large binary value , try to use ‘long’ instead of ‘int’ to avoid any errors.
2. Using pre-defined function
Instead of the approach we can directly use a Java built-in method (i.e. Integer.parseInt()) that converts a string representation of an integer to its actual integer value.
Below is the implementation of the above method:
Java
import java.util.*;
class GFG {
public static void main(String args[])
{
int num, ans;
String temp;
num = 1010 ;
temp = String.valueOf(num);
ans = Integer.parseInt(temp, 2 );
System.out.println("Decimal Equivalent of " + num
+ " is " + ans);
}
}
|
Output
Decimal Equivalent of 1010 is 10
Complexity of the above method:
Time complexity : O(n)
Auxiliary Space : O(1)
Here n is total number of digits in the given binary number.
If you are dealing with large binary value , try to use ‘long’ instead of ‘int’ to avoid any errors.
3. Using Bitwise operators
We can also use << to perform the above operations. The >> operator in Java is the right shift operator. It shifts the bits of a number to the right by a specified number of positions. This operator generally means :
x>>y = x/2^y
- 16 >> 2 = 4
- 4 >> 2 = 1
- 8 >> 2 = 2
Below shown a Java Code for the above mentioned approach :
Java
import java.util.*;
class GFG {
public static void binaryToDecimal( int num)
{
int r, temp, ans = 0 , p = 0 ;
temp = num;
while (num != 0 ) {
r = num % 10 ;
ans += r * ( 1 << p);
p++;
num /= 10 ;
}
System.out.println("Decimal Equivalent of " + temp
+ " is " + ans);
}
public static void main(String[] args)
{
int num = 1100 ;
binaryToDecimal(num);
}
}
|
Output :
Enter the Binary number : 1100
Decimal Equivalent of 1100 is = 12
Complexity of the above method:
Time complexity : O(n)
Auxiliary Space : O(1)
Here n is total number of digits in the given binary number.
Share your thoughts in the comments
Please Login to comment...