Binary to Decimal Conversion in Java

Binary to Decimal Conversion

Binary to Decimal conversion is the process of converting a binary number (base-2) into its equivalent decimal number (base-10). Each binary digit represents a power of 2.

For example, the binary number 1010 is equal to decimal 10 because:

(1 × 2³) + (0 × 2²) + (1 × 2¹) + (0 × 2⁰) = 8 + 0 + 2 + 0 = 10

We will explore three methods to convert a binary number to a decimal number using Java programming.

Method 1: Using Loop

import java.util.Scanner;

public class Main {
    public static int binaryToDecimal(long binary) {
        int decimal = 0, i = 0;
        while (binary != 0) {
            int lastDigit = (int) (binary % 10);
            decimal += lastDigit * Math.pow(2, i);
            binary /= 10;
            i++;
        }
        return decimal;
    }
}
            

Output:

Enter a binary number: 1010
Decimal equivalent (Loop): 10

Method 2: Using Recursion

public class Main {
    public static int binaryToDecimalRecursive(long n, int i) {
        if (n == 0)
            return 0;
        return (int)((n % 10) * Math.pow(2, i) + binaryToDecimalRecursive(n / 10, i + 1));
    }
}
            

Output:

Enter a binary number: 1010
Decimal equivalent (Recursion): 10

Method 3: Using Bitwise Operations

public class Main {
    public static int binaryToDecimalBitwise(int n) {
        int decimal = 0, base = 1;
        while (n > 0) {
            int lastDigit = n % 10;
            decimal += lastDigit * base;
            base *= 2;
            n /= 10;
        }
        return decimal;
    }
}
            

Output:

Enter a binary number: 1010
Decimal equivalent (Bitwise): 10

Main Function

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a binary number: ");
        long binary = scanner.nextLong();
        
        System.out.println("Decimal equivalent (Loop): " + binaryToDecimal(binary));
        System.out.println("Decimal equivalent (Recursion): " + binaryToDecimalRecursive(binary, 0));
        System.out.println("Decimal equivalent (Bitwise): " + binaryToDecimalBitwise((int) binary));
        
        scanner.close();
    }
}
            
Numbers

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

HCF - Highest Common Factor: C C++ Java Python

LCM - Lowest Common Multiple: C C++ Java Python

GCD - Greatest Common Divisor: C C++ Java Python

Binary to Decimal Conversion: C C++ Java Python

Octal to Decimal Conversion: C C++ Java Python

Hexadecimal to Decimal Conversion: C C++ Java Python

Decimal to Binary Conversion: C C++ Java Python

Decimal to Octal Conversion: C C++ Java Python

Decimal to Hexadecimal Conversion: C C++ Java Python

Binary to Octal Conversion: C C++ Java Python

Quadrants in which a given coordinate lies: C C++ Java Python

Addition of Two Fractions: C C++ Java Python

Calculate the Area of a Circle: C C++ Java Python

Convert Digit/Number to Words: C C++ Java Python

Finding Roots of a Quadratic Equation: C C++ Java Python