Open In App

Binary to Decimal Conversion in Java

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

  1. Basic Approach
  2. Using pre-defined function
  3. 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




// Java Program to demonstrate
// Basic Approach of
// Binary to Decimal Number
import java.util.*;
 
// Driver Class
class GFG {
    public static void StringBinaryToDecimal(String s)
    {
        int ans = 0, i, p = 0;
 
        // length of String
        int len = s.length();
 
        // Traversing the String
        for (i = len - 1; i >= 0; i--) {
 
            if (s.charAt(i) == '1') {
                // Calculating Decimal Number
                ans += Math.pow(2, p);
            }
            // incrementing value of 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) {
 
            // remainder when num is
            // divided by 10
            rem = num % 10;
 
            // quotient
            num /= 10;
 
            // Calculating decimal number
            ans += rem * Math.pow(2, i);
 
            i++;
        }
 
        System.out.println("Decimal Equivalent of " + temp
                           + " is " + ans);
    }
 
    // main function
    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




// Java Program to demonstrate
// Binary to Decimal Conversion
// Using ParseInt() function
import java.util.*;
 
// Driver Class
class GFG {
    // main function
    public static void main(String args[])
    {
        // Declaring Variables
        int num, ans;
        String temp;
 
        num = 1010;
 
        // Binary to decimal Conversion
        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




// Java Program to implement
// Binary to Decimal Conversion
// Using Bitwise Operators
import java.util.*;
 
// Driver Class
class GFG {
 
    public static void binaryToDecimal(int num)
    {
        int r, temp, ans = 0, p = 0;
        // Store the original binary number for later
        // display
        temp = num;
 
        // Loop to convert binary to decimal
        while (num != 0) {
            // Get the rightmost digit of the binary number
            r = num % 10;
 
            // Update the decimal equivalent using bitwise
            // left shift
            ans += r * (1 << p);
 
            // Move to the next position
            p++;
 
            // Remove the rightmost digit from the binary
            // number
            num /= 10;
        }
 
        System.out.println("Decimal Equivalent of " + temp
                           + " is " + ans);
    }
 
    // main function
    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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads