TheAlgorithms/Java · error · IllegalArgumentException
Invalid BCD digit: {}
Error message
Invalid BCD digit: {} What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java:45
* <p>1. Validate the BCD number to ensure all digits are between 0 and 9.
* <p>2. Extract the last 4 bits (one BCD digit) from the BCD number.
* <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number.
* <p>4. Shift the BCD number right by 4 bits to process the next BCD digit.
* <p>5. Repeat steps 1-4 until the BCD number is zero.
*
* @param bcd The BCD number.
* @return The corresponding decimal number.
* @throws IllegalArgumentException if the BCD number contains invalid digits.
*/
public static int bcdToDecimal(int bcd) {
int decimal = 0;
int multiplier = 1;
// Validate BCD digits
while (bcd > 0) {
int digit = bcd & 0xF;
if (digit > 9) {
throw new IllegalArgumentException("Invalid BCD digit: " + digit);
}
decimal += digit * multiplier;
multiplier *= 10;
bcd >>= 4;
}
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.View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before BcdConversion.bcdToDecimal(0x1A); // nibble A(10) -> throws // after // ensure value is BCD; e.g. 0x12 represents decimal 12 BcdConversion.bcdToDecimal(0x12); // returns 12
Defensive patterns
Strategy: validation
Validate before calling
public static boolean isValidBcd(int bcd) {
int v = bcd;
while (v > 0) {
if ((v & 0xF) > 9) return false;
v >>>= 4;
}
return true;
}
// usage
if (!isValidBcd(bcd)) throw new IllegalArgumentException("not a BCD value");
BcdConversion.bcdToDecimal(bcd); Type guard
public static boolean isValidBcd(int bcd) {
int v = bcd;
while (v > 0) {
if ((v & 0xF) > 9) return false;
v >>>= 4;
}
return true;
} Try / catch
try {
return BcdConversion.bcdToDecimal(bcd);
} catch (IllegalArgumentException e) {
// value was not BCD-encoded; treat as parse failure
return -1;
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Value out of bounds for BCD representation: {}
- Shift amount cannot be negative: {}
- Bit positions must be between 0 and 31
- Input must be non-negative
- Alpha must be between 0 and 1.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/a9428168e5201f39.
Report an issue: GitHub.