{"record":{"id":"a9428168e5201f39","repo":"TheAlgorithms/Java","slug":"invalid-bcd-digit","errorCode":null,"errorMessage":"Invalid BCD digit: {}","messagePattern":"Invalid BCD digit: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java","lineNumber":45,"sourceCode":"     * <p>1. Validate the BCD number to ensure all digits are between 0 and 9.\n     * <p>2. Extract the last 4 bits (one BCD digit) from the BCD number.\n     * <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number.\n     * <p>4. Shift the BCD number right by 4 bits to process the next BCD digit.\n     * <p>5. Repeat steps 1-4 until the BCD number is zero.\n     *\n     * @param bcd The BCD number.\n     * @return The corresponding decimal number.\n     * @throws IllegalArgumentException if the BCD number contains invalid digits.\n     */\n    public static int bcdToDecimal(int bcd) {\n        int decimal = 0;\n        int multiplier = 1;\n\n        // Validate BCD digits\n        while (bcd > 0) {\n            int digit = bcd & 0xF;\n            if (digit > 9) {\n                throw new IllegalArgumentException(\"Invalid BCD digit: \" + digit);\n            }\n            decimal += digit * multiplier;\n            multiplier *= 10;\n            bcd >>= 4;\n        }\n        return decimal;\n    }\n\n    /**\n     * Converts a decimal number to BCD (Binary-Coded Decimal).\n     * <p>Steps:\n     * <p>1. Check if the decimal number is within the valid range for BCD (0 to 9999).\n     * <p>2. Extract the last decimal digit from the decimal number.\n     * <p>3. Shift the digit to the correct BCD position and add it to the BCD number.\n     * <p>4. Remove the last decimal digit from the decimal number.\n     * <p>5. Repeat steps 2-4 until the decimal number is zero.\n     *\n     * @param decimal The decimal number.","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java#L27-L63","documentation":"Thrown by BcdConversion.bcdToDecimal(bcd) when a 4-bit nibble extracted from the input exceeds 9. Binary-Coded Decimal encodes each decimal digit in one nibble, so valid nibble values are 0-9; a nibble of 10-15 is not a valid BCD digit and cannot be decoded to a decimal number.","triggerScenarios":"Calling `bcdToDecimal(0x000A)` (nibble 10), `bcdToDecimal(0x00FF)`, or any value where `bcd & 0xF > 9` at any shift step. The loop masks the lowest nibble and right-shifts by 4 repeatedly until the input is exhausted.","commonSituations":"Passing a plain hex/binary integer that is not actually BCD-encoded (e.g. 0xFF interpreted as BCD); feeding raw sensor/protocol data that wasn't validated as BCD; byte-order or endianness confusion when packing nibbles.","solutions":["Confirm the input is genuinely BCD-encoded (each nibble 0-9), not a plain binary number.","Pre-validate each nibble before calling, or sanitize the input by masking out invalid nibbles if appropriate.","Check the data source/protocol that produced the value for encoding assumptions."],"exampleFix":"// before\nBcdConversion.bcdToDecimal(0x1A);  // nibble A(10) -> throws\n\n// after\n// ensure value is BCD; e.g. 0x12 represents decimal 12\nBcdConversion.bcdToDecimal(0x12);  // returns 12","handlingStrategy":"validation","validationCode":"public static boolean isValidBcd(int bcd) {\n    int v = bcd;\n    while (v > 0) {\n        if ((v & 0xF) > 9) return false;\n        v >>>= 4;\n    }\n    return true;\n}\n// usage\nif (!isValidBcd(bcd)) throw new IllegalArgumentException(\"not a BCD value\");\nBcdConversion.bcdToDecimal(bcd);","typeGuard":"public static boolean isValidBcd(int bcd) {\n    int v = bcd;\n    while (v > 0) {\n        if ((v & 0xF) > 9) return false;\n        v >>>= 4;\n    }\n    return true;\n}","tryCatchPattern":"try {\n    return BcdConversion.bcdToDecimal(bcd);\n} catch (IllegalArgumentException e) {\n    // value was not BCD-encoded; treat as parse failure\n    return -1;\n}","preventionTips":["Confirm data is BCD-encoded, not plain binary, before decoding.","Validate nibbles 0..9 at the protocol boundary.","Watch for endianness/nibble-order mistakes when packing."],"tags":["bit-manipulation","bcd","encoding","argument-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}