{"record":{"id":"2d355554529b09c7","repo":"TheAlgorithms/Java","slug":"incorrect-binary-digit","errorCode":null,"errorMessage":"Incorrect binary digit: {}","messagePattern":"Incorrect binary digit: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java","lineNumber":26,"sourceCode":"\n    private BinaryToDecimal() {\n    }\n\n    /**\n     * Converts a binary number to its decimal equivalent.\n     *\n     * @param binaryNumber The binary number to convert.\n     * @return The decimal equivalent of the binary number.\n     * @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.\n     */\n    public static long binaryToDecimal(long binaryNumber) {\n        long decimalValue = 0;\n        long power = 0;\n\n        while (binaryNumber != 0) {\n            long digit = binaryNumber % 10;\n            if (digit > 1) {\n                throw new IllegalArgumentException(\"Incorrect binary digit: \" + digit);\n            }\n            decimalValue += (long) (digit * Math.pow(BINARY_BASE, power++));\n            binaryNumber /= 10;\n        }\n        return decimalValue;\n    }\n\n    /**\n     * Converts a binary String to its decimal equivalent using bitwise operators.\n     *\n     * @param binary The binary number to convert.\n     * @return The decimal equivalent of the binary number.\n     * @throws IllegalArgumentException If the binary number contains digits other than 0 and 1.\n     */\n    public static long binaryStringToDecimal(String binary) {\n        boolean isNegative = binary.charAt(0) == '-';\n        if (isNegative) {\n            binary = binary.substring(1);","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/conversions/BinaryToDecimal.java#L8-L44","documentation":"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.","triggerScenarios":"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.","commonSituations":"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'.","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."],"exampleFix":"// before\nlong dec = BinaryToDecimal.binaryToDecimal(102); // '2' digit rejected\n\n// after\nlong dec = BinaryToDecimal.binaryStringToDecimal(\"1010\"); // string preserves digits","handlingStrategy":"validation","validationCode":"if (!String.valueOf(binaryNumber).matches(\"[01]+\")) {\n    throw new IllegalArgumentException(\"not a binary number: \" + binaryNumber);\n}\nlong dec = BinaryToDecimal.binaryToDecimal(binaryNumber);","typeGuard":"static boolean isBinaryDigits(long n) {\n    return String.valueOf(n).matches(\"[01]+\");\n}","tryCatchPattern":"try {\n    long dec = BinaryToDecimal.binaryToDecimal(n);\n} catch (IllegalArgumentException e) {\n    throw new DomainException(\"Invalid binary input: \" + n, e);\n}","preventionTips":["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."],"tags":["conversions","binary","validation","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}