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 : 12Input : 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
// 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);
}
}
OutputDecimal 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);
}
}
OutputDecimal 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.
Similar Reads
How to convert binary to decimal in JavaScript?
A binary number is a number expressed in the binary numeral system, which uses only two symbols 0 and 1. Decimal numbers are base 10 in a JavaScript number system that uses 10 digits from 0 to 9. We are given a number as Binary input we have to convert it into decimal in JavaScript and print the res
3 min read
Binary to Octal Conversion
Number system is an important part of mathematics. The number system and its conversion is used in various fields of mathematics and computer science. In this article we will explore binary, octal and conversion of binary to octal number system. What is Binary Number System?Number system with its ba
4 min read
Binary to Decimal Converter
Binary to Decimal Converter is a free online tool to convert binary to decimal. Converting between binary to decimal is a common task in everyday life. Here, GeeksforGeeks provides a free user-friendly, and efficient online binary decimal Conversion tool to simplify this process and ensure accuracy.
8 min read
How to convert Binary to Hexadecimal?
A number system is a way of expressing numbers using a consistent set of symbols or digits. The most common number systems are based on different "bases" or "radices," which determine how numbers are represented.Here are some common types of number systems:Decimal Number System (base 10; uses digits
6 min read
Hex to Decimal Conversion
Hex to Decimal Conversion is a fundamental operation in computer science and digital electronics. The operation requires converting numerals from one number system to another, specifically from the Hexadecimal Number System to the Decimal Number System. As we know, a number system is used to represe
12 min read
Converting Binary To Octal number using HashMap in Java
Remember while we were converting octal to decimal we were taking 3 binary digits at a time. A similar approach will be used where here for every 3 digits we are having a corresponding number as in octal system we have numbers from 0 to 'R-1' where R represents base value of number system. As the na
3 min read
DecimalFormat applyPattern() method in Java
The applyPattern() Method of DecimalFormat class in Java is used to apply a given string pattern to this Decimal format. This pattern can be set by the user. Syntax: public void applyPattern(String pattern) Parameters: The method takes one parameter pattern of String type and refers to the pattern w
1 min read
BigDecimal ulp() Method in Java
The java.math.BigDecimal.ulp() is an inbuilt method in Java that returns the size of an ulp(unit in the last place) of this BigDecimal. An ulp of a nonzero BigDecimal value is defined as the positive distance between this value and the BigDecimal value next larger in magnitude with the same number o
2 min read
DuoDecimal in Java
Duodecimal represents a system which is a notation system in which a number having its base as 12 is called a duodecimal number. In java, we can use to find the conversation of duodecimal numbers into respective binary, octal, decimal, hexadecimal number, or any other base numbers. In java, we can u
4 min read
Convert String to Double in Java
In this article, we will convert a String to a Double in Java. There are three methods for this conversion from String to Double, as mentioned below in the article.Methods for String-to-Double ConversionDifferent ways for converting a String to a Double are mentioned below:Using the parseDouble() me
3 min read