TheAlgorithms/Java · error · IllegalArgumentException
Incorrect binary digit: {}
Error message
Incorrect binary digit: {} What it means
Thrown by BinaryToDecimal.binaryToDecimal(long) when a decimal digit of the input is greater than 1. The method treats the input as a sequence of decimal digits that must each be 0 or 1; any digit 2-9 is rejected. Note this validates decimal digits of a long, not binary bits, so leading zeros are lost and the input is a decimal number whose digits happen to be 0/1.
Source
Thrown at src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java:26
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;
}
/**
* Converts a binary String to its decimal equivalent using bitwise operators.
*
* @param binary 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 binaryStringToDecimal(String binary) {
boolean isNegative = binary.charAt(0) == '-';
if (isNegative) {
binary = binary.substring(1);View on GitHub (pinned to fdfb9a395b)
Solutions
- Validate the input with String.valueOf(n).matches("[01]+") before calling.
- Use the binaryStringToDecimal(String) overload if you have a string representation, preserving leading zeros.
- Use Integer/Long.parseInt(input, 2) for parsing binary strings directly.
Example fix
// before
long dec = BinaryToDecimal.binaryToDecimal(102); // '2' digit rejected
// after
long dec = BinaryToDecimal.binaryStringToDecimal("1010"); // string preserves digits Defensive patterns
Strategy: validation
Validate before calling
if (!String.valueOf(binaryNumber).matches("[01]+")) {
throw new IllegalArgumentException("not a binary number: " + binaryNumber);
}
long dec = BinaryToDecimal.binaryToDecimal(binaryNumber); Type guard
static boolean isBinaryDigits(long n) {
return String.valueOf(n).matches("[01]+");
} Try / catch
try {
long dec = BinaryToDecimal.binaryToDecimal(n);
} catch (IllegalArgumentException e) {
throw new DomainException("Invalid binary input: " + n, e);
} Prevention
- Use binaryStringToDecimal(String) to preserve leading zeros.
- Validate the long's decimal digits are only 0/1 before calling.
- Prefer Long.parseLong(s, 2) for string-sourced binary.
When it happens
Trigger: Passing 102 (contains '2'), 123, or any number with a digit > 1. Passing a number that was meant to be parsed as a string but was passed as a long, losing leading zeros. Passing a negative number whose last digit mod 10 exceeds 1.
Common situations: Confusing the long representation with the digit string (e.g., 0b10 vs 10). Numbers with leading zeros that collapse when stored as long. User input containing typos like '12' instead of '10'.
Related errors
- Incorrect binary digit: {}
- Input is not a valid binary number.
- Slope and intercept must be valid numbers.
- For input string: {}
- Input cannot be null
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/2d355554529b09c7.
Report an issue: GitHub.