Converting binary numbers to decimal is a common task in programming, especially when working with low-level operations or in areas like data processing and computer architecture. Java provides several ways to achieve this conversion, whether you prefer to use built-in methods or implement the logic manually.
In this article, we’ll explore different methods to convert a binary number (base 2) to its decimal (base 10) equivalent in Java.
Method 1: Using Integer.parseInt()
The easiest and most efficient way to convert a binary string to a decimal number in Java is by using the parseInt()
method from the Integer
class. This method allows you to specify the base of the input string, and it will return the corresponding decimal value.
Example:
public class BinaryToDecimal {
public static void main(String[] args) {
String binaryString = "1101"; // Binary number
int decimal = Integer.parseInt(binaryString, 2); // Convert binary to decimal
System.out.println("Decimal value: " + decimal);
}
}
Explanation:
- The
parseInt()
method takes two arguments: the binary string and the base (which is 2 for binary). - It then returns the corresponding decimal integer.
Output:
Decimal value: 13
In this example, the binary number 1101
is equivalent to the decimal number 13
.
Method 2: Manually Converting Binary to Decimal
If you prefer to manually convert binary to decimal, you can use the position of each bit in the binary number. The binary number 1101
can be expanded as:
(1 * 2^3) + (1 * 2^2) + (0 * 2^1) + (1 * 2^0)
This gives us the decimal value 13
. You can write a custom method to convert binary to decimal by iterating through the binary string from right to left.
Example:
public class BinaryToDecimal {
public static void main(String[] args) {
String binaryString = "1101"; // Binary number
int decimal = binaryToDecimal(binaryString); // Convert binary to decimal
System.out.println("Decimal value: " + decimal);
}
public static int binaryToDecimal(String binary) {
int decimal = 0;
int length = binary.length();
// Iterate through each character in the binary string
for (int i = 0; i < length; i++) {
char bit = binary.charAt(i);
// Convert each bit to its decimal equivalent
if (bit == '1') {
decimal += Math.pow(2, length - i - 1); // 2^position
}
}
return decimal;
}
}
Explanation:
- We loop through each character of the binary string.
- For each ‘1’ in the string, we calculate its decimal equivalent using the formula
2^position
and add it to the running total. Math.pow(2, length - i - 1)
computes the power of 2 based on the bit’s position.
Output:
Decimal value: 13
Method 3: Using a for
Loop and Bitwise Operators
For even more control over the conversion process, you can use bitwise operators. This method involves shifting the bits and checking if the bit is set (i.e., whether it is 1 or 0).
Example:
public class BinaryToDecimal {
public static void main(String[] args) {
String binaryString = "1101"; // Binary number
int decimal = binaryToDecimal(binaryString); // Convert binary to decimal
System.out.println("Decimal value: " + decimal);
}
public static int binaryToDecimal(String binary) {
int decimal = 0;
// Iterate through each character in the binary string
for (int i = 0; i < binary.length(); i++) {
char bit = binary.charAt(i);
// Shift the decimal value left and add the bit's value
decimal = (decimal << 1) | (bit - '0');
}
return decimal;
}
}
Explanation:
- We loop through each character in the binary string and use the left shift operator
<<
to shift the current decimal value. - The bitwise OR operator
|
is used to add the current bit’s value (either 0 or 1) to the decimal result.
Output:
Decimal value: 13
Method 4: Using BigInteger Class (for Large Numbers)
For very large binary numbers that exceed the range of a regular int
, you can use the BigInteger
class in Java. This class can handle arbitrarily large numbers and is particularly useful for operations with large datasets or cryptographic applications.
Example:
import java.math.BigInteger;
public class BinaryToDecimal {
public static void main(String[] args) {
String binaryString = "1101010101011101011010101101"; // Large binary number
BigInteger decimal = new BigInteger(binaryString, 2); // Convert binary to decimal
System.out.println("Decimal value: " + decimal);
}
}
Explanation:
- The
BigInteger
constructor takes the binary string and the base (2 for binary) and returns the corresponding decimal value.
Output:
Decimal value: 3483011501
Converting binary to decimal is an essential skill in programming, especially when dealing with data manipulation or low-level system operations. In Java, you can easily convert binary to decimal using built-in methods like Integer.parseInt()
, or you can write your own conversion logic using loops, bitwise operators, or even the BigInteger
class for large numbers.
By understanding these methods, you can handle binary-to-decimal conversion efficiently in a variety of situations.