-
Notifications
You must be signed in to change notification settings - Fork 19.7k
/
Copy pathBinaryToDecimal.java
33 lines (29 loc) · 1002 Bytes
/
BinaryToDecimal.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.thealgorithms.conversions;
/**
* This class converts a Binary number to a Decimal number
*/
final class BinaryToDecimal {
private static final int BINARY_BASE = 2;
private BinaryToDecimal() {
}
/**
* Converts a binary number to its decimal equivalent.
*
* @param binaryNumber The binary number to convert.
* @return The decimal equivalent of the binary number.
* @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.
*/
public static long binaryToDecimal(long binaryNumber) {
long decimalValue = 0;
long power = 0;
while (binaryNumber != 0) {
long digit = binaryNumber % 10;
if (digit > 1) {
throw new IllegalArgumentException("Incorrect binary digit: " + digit);
}
decimalValue += (long) (digit * Math.pow(BINARY_BASE, power++));
binaryNumber /= 10;
}
return decimalValue;
}
}