How to Convert Binary Number to Decimal Number in Java

Nitish Kumar Singh

Jan 15, 2025

Converting a binary number to a decimal number is a basic operation in coding that helps us understand binary numbers and their connection to decimal numbers. In this post, we will learn what a binary number is, the steps to convert it to decimal, and write a Java method to perform this task.

Image Generated by Chat GPT

What is a Binary Number?

A binary number is a number expressed in the base-2 numeral system, which uses only two digits: 0 and 1. Each digit in a binary number is referred to as a bit, and the value contribution of each bit in decimal conversion depends on its position. This value is the product of the digit itself and a power of 2, with powers starting from 0 at the right side. For example, the binary number 1011 can be expressed in decimal as:

1 × 2³ + 0 × 2² + 1 × 2¹ + 1 × 2⁰ = 8 + 0 + 2 + 1 = 11

For example, the extended form of the decimal number 78957 is as below:

7 × 10⁴ + 8 × 10³ + 9 × 10² + 5 × 10¹ + 7 × 10⁰

In a decimal number, each digit has a place value of the product of the digit itself and a power of 10, with powers increasing from 0 at the right side. In the decimal system, the base is 10, but in the binary system, it is 2.

Therefore, the number system with base 10 (decimal) is called the decimal or denary number system, and the number system with base 2 (binary) is called the binary number system.

To convert a binary number to a decimal number, we need to take each digit from that number starting from the right side, multiply it by a power of 2 (starting from 0), repeat this for all digits, and add all values to get the final decimal number.

Here question is that how to get last digit from a number and remove that digit from it? so we can perform above operations for all digits.

How to Get the Last Digit From a Number

To get the last digit of a number, we divide it by 10, and the remainder is our last digit. For example, when we divide 89 by 10, the remainder is 9, which is the last digit of 89. We can get the last digit in Java using the modulus operator as shown below:

int number = 89;
int lastDigit = number % 10;

How to Remove the Last Digit From a Number

We can remove the last digit from a number by dividing it by 10. For example, when we divide 89 by 10, the result is 8, which is the number after removing 9 from 89.

int number = 89;
int numberAfterRemovingLastDigit = number / 10;

Putting It All Together

To convert a binary number to decimal, we need to:

  1. Get the last digit of the binary number.
  2. Multiply it by a power of 2.
  3. Add it to a variable storing the decimal value.
  4. Remove the last digit from the binary number.

We repeat these steps for all digits. To achieve this, we run a while loop until the given binary number becomes 0. By dividing the binary number by 10 in each loop iteration, it eventually becomes 0 after n iterations, where n is the number of digits in the binary number.

Java Method: binaryToDecimal

Finally, all our above logical discussions result in a Java method binaryToDecimal, as shown in the code below.

public class JavaBasics {
    public static void main(String[] aStrings){
        System.out.println(binaryToDecimal(0));
        System.out.println(binaryToDecimal(01));
        System.out.println(binaryToDecimal(10));
        System.out.println(binaryToDecimal(11));
        System.out.println(binaryToDecimal(100));
        System.out.println(binaryToDecimal(101));
        System.out.println(binaryToDecimal(110));
        System.out.println(binaryToDecimal(111));
        System.out.println(binaryToDecimal(1000));
    }
    public static int binaryToDecimal(long binary){
        if(binary < 0 || !(""+binary).matches("[01]+")) throw new IllegalArgumentException("Invalid binary number");
        int decimal = 0, power = 0;

        while (binary>0) {
            int lastDigit = (int) binary % 10;
            decimal += lastDigit * (int) Math.pow(2, power);
            binary = binary / 10;
            power++;
        }
        return decimal;
    }
}

In the above code, the first line of the binaryToDecimal method checks if the given binary number is valid or not and processes it accordingly.

This is how we can write a method in Java to convert a binary number to a decimal number. I hope you found this post helpful, informative, and enjoyable to read. Happy Coding!

Published on Jan 15, 2025
Comments (undefined)

Read More

Mastering Google Sheets: Solving Real-World Problems Beyond VLOOKUP

Hi everyone! In this blog post, I will share a question and its solution that I encountered during an interview. This experience enhanced my understanding of the VLOOKUP function in Google Sheets, taking my knowledge from basic to advanced levels.

Nov 21, 2024

Analyzing Code Context in Monaco Editor for Custom Suggestions

Hi Everyone! In this post, we are going to understand and write code to find the cursor position and analyze the code being written by the user in the Monaco Web Code Editor to provide suggestions accordingly.

Nov 17, 2024

How to Highlight Groups of Identical Text with Unique Colors Using JavaScript in Google Sheets

Hi Everyone! In this post, we are going to add a feature in Google Sheets that highlights groups of cells that have the same text with unique colors by creating a custom menu using Google Apps Script.

Sep 23, 2024

How to Run Custom JavaScript Code in Google Sheets Using Apps Script

Hi everyone! In this blog post, we are going to explore how we can Run custom JavaScript code in Google Sheets to perform custom calculations, process data to meet our own needs, and do many more things.

Sep 11, 2024

How to Edit and Deploy Our Website Code Using Only a Mobile

Hello everyone, in this blog post, we are going to uncover the power of smartphones to do coding.

Jul 22, 2024

How to Generate Unique IDs in JavaScript

In the modern world, where data is the most important aspect of a profitable business in the tech field, managing data efficiently and processing it in the right direction is crucial. When we have to manage, organize, and process a large amount of data records, unique identifiers (or IDs) become a necessity.

Mar 15, 2024