TheAlgorithms/Java · error · IllegalArgumentException
Value out of bounds for BCD representation: {}
Error message
Value out of bounds for BCD representation: {} What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java:69
return decimal;
}
/**
* Converts a decimal number to BCD (Binary-Coded Decimal).
* <p>Steps:
* <p>1. Check if the decimal number is within the valid range for BCD (0 to 9999).
* <p>2. Extract the last decimal digit from the decimal number.
* <p>3. Shift the digit to the correct BCD position and add it to the BCD number.
* <p>4. Remove the last decimal digit from the decimal number.
* <p>5. Repeat steps 2-4 until the decimal number is zero.
*
* @param decimal The decimal number.
* @return The corresponding BCD number.
* @throws IllegalArgumentException if the decimal number is greater than 9999.
*/
public static int decimalToBcd(int decimal) {
if (decimal < 0 || decimal > 9999) {
throw new IllegalArgumentException("Value out of bounds for BCD representation: " + decimal);
}
int bcd = 0;
int shift = 0;
while (decimal > 0) {
int digit = decimal % 10;
bcd |= (digit << (shift * 4));
decimal /= 10;
shift++;
}
return bcd;
}
}
View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before
BcdConversion.decimalToBcd(12345); // throws
// after
if (decimal < 0 || decimal > 9999) throw new IllegalArgumentException("need 0..9999");
BcdConversion.decimalToBcd(decimal); Defensive patterns
Strategy: validation
Validate before calling
public static boolean inBcdRange(int decimal) {
return decimal >= 0 && decimal <= 9999;
}
// usage
if (!inBcdRange(decimal)) throw new IllegalArgumentException("decimal must be 0..9999");
BcdConversion.decimalToBcd(decimal); Type guard
public static boolean inBcdRange(int decimal) {
return decimal >= 0 && decimal <= 9999;
} Try / catch
try {
return BcdConversion.decimalToBcd(decimal);
} catch (IllegalArgumentException e) {
// out of 4-digit BCD range; fall back to wider representation
return encodeBcdWide(decimal);
} Prevention
- Constrain inputs to 0..9999 (four decimal digits).
- Use a wider BCD scheme for larger values.
- Validate at the form/config boundary.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Invalid BCD digit: {}
- Bit positions must be between 0 and 31
- Shift amount cannot be negative: {}
- Input must be non-negative
- Input cannot be negative
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/71ae89171c3493d9.
Report an issue: GitHub.