package com.thealgorithms.bitmanipulation; /** * This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers. * * BCD is a class of binary encodings of decimal numbers where each decimal digit is represented by a fixed number of binary digits, usually four or eight. * * For more information, refer to the * Binary-Coded Decimal Wikipedia page. * * Example usage: *
 * int decimal = BcdConversion.bcdToDecimal(0x1234);
 * System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
 *
 * int bcd = BcdConversion.decimalToBcd(1234);
 * System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
 * 
*/ public final class BcdConversion { private BcdConversion() { } /** * Converts a BCD (Binary-Coded Decimal) number to a decimal number. *

Steps: *

1. Validate the BCD number to ensure all digits are between 0 and 9. *

2. Extract the last 4 bits (one BCD digit) from the BCD number. *

3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number. *

4. Shift the BCD number right by 4 bits to process the next BCD digit. *

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). *

Steps: *

1. Check if the decimal number is within the valid range for BCD (0 to 9999). *

2. Extract the last decimal digit from the decimal number. *

3. Shift the digit to the correct BCD position and add it to the BCD number. *

4. Remove the last decimal digit from the decimal number. *

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; } }