{"record":{"id":"71ae89171c3493d9","repo":"TheAlgorithms/Java","slug":"value-out-of-bounds-for-bcd-representation","errorCode":null,"errorMessage":"Value out of bounds for BCD representation: {}","messagePattern":"Value out of bounds for BCD representation: (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java","lineNumber":69,"sourceCode":"        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.\n     * @return The corresponding BCD number.\n     * @throws IllegalArgumentException if the decimal number is greater than 9999.\n     */\n    public static int decimalToBcd(int decimal) {\n        if (decimal < 0 || decimal > 9999) {\n            throw new IllegalArgumentException(\"Value out of bounds for BCD representation: \" + decimal);\n        }\n\n        int bcd = 0;\n        int shift = 0;\n        while (decimal > 0) {\n            int digit = decimal % 10;\n            bcd |= (digit << (shift * 4));\n            decimal /= 10;\n            shift++;\n        }\n        return bcd;\n    }\n}\n","sourceCodeStart":51,"sourceCodeEnd":83,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java#L51-L83","documentation":"Thrown by BcdConversion.decimalToBcd(decimal) when the input is negative or greater than 9999. The implementation packs at most four decimal digits into four nibbles (16 bits), so only the range 0..9999 fits. Values outside this range cannot be represented in the fixed 4-digit BCD form used here.","triggerScenarios":"Calling `decimalToBcd(-1)`, `decimalToBcd(10000)`, or `decimalToBcd(99999)`. The guard `decimal < 0 || decimal > 9999` rejects both negatives and any value with more than four decimal digits.","commonSituations":"Passing a full year (e.g. 20260) or other 5-digit value; feeding a signed/offset value that can be negative; assuming the method supports arbitrary-precision BCD when it only handles four digits.","solutions":["Clamp or reject the input so that 0 <= decimal <= 9999 before calling.","If larger values are needed, use a wider BCD representation (more nibbles) or a different library.","Validate the input range at the source (form field, parsed config) with a 0..9999 constraint."],"exampleFix":"// before\nBcdConversion.decimalToBcd(12345);  // throws\n\n// after\nif (decimal < 0 || decimal > 9999) throw new IllegalArgumentException(\"need 0..9999\");\nBcdConversion.decimalToBcd(decimal);","handlingStrategy":"validation","validationCode":"public static boolean inBcdRange(int decimal) {\n    return decimal >= 0 && decimal <= 9999;\n}\n// usage\nif (!inBcdRange(decimal)) throw new IllegalArgumentException(\"decimal must be 0..9999\");\nBcdConversion.decimalToBcd(decimal);","typeGuard":"public static boolean inBcdRange(int decimal) {\n    return decimal >= 0 && decimal <= 9999;\n}","tryCatchPattern":"try {\n    return BcdConversion.decimalToBcd(decimal);\n} catch (IllegalArgumentException e) {\n    // out of 4-digit BCD range; fall back to wider representation\n    return encodeBcdWide(decimal);\n}","preventionTips":["Constrain inputs to 0..9999 (four decimal digits).","Use a wider BCD scheme for larger values.","Validate at the form/config boundary."],"tags":["bit-manipulation","bcd","encoding","range-validation","illegalargumentexception"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}