Refactor BCD Conversion docs and add more tests (#5762)

This commit is contained in:
Hardik Pawar
2024-10-13 16:50:20 +05:30
committed by GitHub
parent bae7f89156
commit e291516dc9
2 changed files with 104 additions and 60 deletions

View File

@ -1,56 +1,80 @@
package com.thealgorithms.bitmanipulation;
/**
* This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
* This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers.
*
* Binary-Coded Decimal (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.
* 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
* <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
*
* <b>Example usage:</b>
* <pre>
* int binary = BcdConversion.bcdToBinary(0x1234);
* System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
* int decimal = BcdConversion.bcdToDecimal(0x1234);
* System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
*
* int bcd = BcdConversion.binaryToBcd(1234);
* System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
* int bcd = BcdConversion.decimalToBcd(1234);
* System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
* </pre>
*/
public final class BcdConversion {
private BcdConversion() {
}
/**
* Converts a BCD (Binary-Coded Decimal) number to binary.
* Converts a BCD (Binary-Coded Decimal) number to a decimal number.
* <p>Steps:
* <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 binary number.
* @return The corresponding decimal number.
* @throws IllegalArgumentException if the BCD number contains invalid digits.
*/
public static int bcdToBinary(int bcd) {
int binary = 0;
public static int bcdToDecimal(int bcd) {
int decimal = 0;
int multiplier = 1;
// Validate BCD digits
while (bcd > 0) {
int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit)
binary += digit * multiplier;
int digit = bcd & 0xF;
if (digit > 9) {
throw new IllegalArgumentException("Invalid BCD digit: " + digit);
}
decimal += digit * multiplier;
multiplier *= 10;
bcd >>= 4; // Shift right by 4 bits to process the next BCD digit
bcd >>= 4;
}
return binary;
return decimal;
}
/**
* Converts a binary number to BCD (Binary-Coded 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 binary The binary number.
* @param decimal The decimal number.
* @return The corresponding BCD number.
* @throws IllegalArgumentException if the decimal number is greater than 9999.
*/
public static int binaryToBcd(int binary) {
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 (binary > 0) {
int digit = binary % 10; // Extract the last decimal digit
bcd |= (digit << (shift * 4)); // Shift the digit to the correct BCD position
binary /= 10; // Remove the last decimal digit
while (decimal > 0) {
int digit = decimal % 10;
bcd |= (digit << (shift * 4));
decimal /= 10;
shift++;
}
return bcd;