Convert Binary to Decimal in Java

Topic

In this program, we’ll learn how to convert a binary number (like 1011) into its decimal equivalent using Java. Binary numbers are made up of only 0s and 1s, and each digit represents a power of 2. Java provides different ways to perform this conversion, including manual calculation using loops or using built-in methods.

Examples

Example 1:
Input: 1011
Output: 11
Explanation: 1×2^3 + 0×2^2 + 1×2^1 + 1×2^0 = 8 + 0 + 2 + 1 = 11

Example 2 (Edge Case):
Input: 0
Output: 0
Explanation: All bits are zero.

Example 3:
Input: 111111
Output: 63
Explanation: Sum of powers of 2 from 0 to 5.

Interviewer Expectations

The interviewer wants to assess your understanding of:

  • Number systems (binary and decimal)
  • Looping through a string or number
  • Calculating powers and position values
  • Using Java built-in methods like Integer.parseInt() (optional)
They are also evaluating if you can explain your logic clearly, handle input as a string or number, and manage edge cases.

Approach

Step-by-step approach:

  1. Read the binary number as a String so that we can iterate through each digit.
  2. Initialize a variable to store the decimal result.
  3. Loop through the string from right to left (starting from the least significant bit).
  4. For each digit, calculate its decimal contribution using: bit * 2position.
  5. Add the value to the result.

Dry Run Example (Binary: 1011)

Binary: 1011

  • Index 3 (rightmost): 1 × 2⁰ = 1
  • Index 2: 1 × 2¹ = 2
  • Index 1: 0 × 2² = 0
  • Index 0: 1 × 2³ = 8

Total = 8 + 0 + 2 + 1 = 11

Java Program

public class BinaryToDecimal {
    public static void main(String[] args) {
        String binaryStr = "1011";
        int decimal = 0;

        for (int i = 0; i < binaryStr.length(); i++) {
            int bit = Character.getNumericValue(binaryStr.charAt(binaryStr.length() - 1 - i));
            decimal += bit * Math.pow(2, i);
        }

        System.out.println("Decimal value: " + decimal);
    }
}
Decimal value: 11

Possible Followup Questions with Answers

1. Can we convert binary to decimal using a built-in method?

Yes. Java provides Integer.parseInt(binaryStr, 2).

String binaryStr = "1011";
int decimal = Integer.parseInt(binaryStr, 2);
System.out.println(decimal); // Output: 11

2. How would you handle invalid binary input?

Validate that the string only contains 0s and 1s using a regular expression:

if (!binaryStr.matches("[01]+")) {
    System.out.println("Invalid binary number.");
}

3. What is the time complexity of your solution?

Time complexity is O(n), where n is the length of the binary string.

4. How can you convert a decimal number to binary?

Use Integer.toBinaryString(decimalNumber) in Java.

int number = 11;
String binary = Integer.toBinaryString(number);
System.out.println(binary); // Output: 1011

Comments

💬 Please keep your comment relevant and respectful. Avoid spamming, offensive language, or posting promotional/backlink content.
All comments are subject to moderation before being published.


Loading comments...