From 7780738ef70bc31888d592c791095c0e247dd71c Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 12:58:01 +0530 Subject: [PATCH 1/5] Refactor BCD Conversion docs and add more tests --- .../bitmanipulation/BcdConversion.java | 23 +++++-- .../bitmanipulation/BcdConversionTest.java | 62 ++++++++++++------- 2 files changed, 58 insertions(+), 27 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java index 3cf8411816c7..2275893fc1cf 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java @@ -3,7 +3,7 @@ /** * This class provides methods to convert between BCD (Binary-Coded Decimal) and binary. * - * 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 * Binary-Coded Decimal Wikipedia page. @@ -20,8 +20,14 @@ public final class BcdConversion { private BcdConversion() { } + /** * Converts a BCD (Binary-Coded Decimal) number to binary. + *

Steps: + *

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

2. Multiply the extracted digit by the corresponding power of 10 and add it to the binary number. + *

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

4. Repeat steps 1-3 until the BCD number is zero. * * @param bcd The BCD number. * @return The corresponding binary number. @@ -30,16 +36,21 @@ public static int bcdToBinary(int bcd) { int binary = 0; int multiplier = 1; while (bcd > 0) { - int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit) + int digit = bcd & 0xF; binary += digit * multiplier; multiplier *= 10; - bcd >>= 4; // Shift right by 4 bits to process the next BCD digit + bcd >>= 4; } return binary; } /** * Converts a binary number to BCD (Binary-Coded Decimal). + *

Steps: + *

1. Extract the last decimal digit from the binary number. + *

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

3. Remove the last decimal digit from the binary number. + *

4. Repeat steps 1-3 until the binary number is zero. * * @param binary The binary number. * @return The corresponding BCD number. @@ -48,9 +59,9 @@ public static int binaryToBcd(int binary) { 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 + int digit = binary % 10; + bcd |= (digit << (shift * 4)); + binary /= 10; shift++; } return bcd; diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java index 85221d92ad42..70d6b58fb240 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java @@ -1,65 +1,85 @@ package com.thealgorithms.bitmanipulation; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; -/** - * Unit tests for the BcdConversion class. - */ public class BcdConversionTest { - /** - * Test the bcdToBinary method with a BCD number. - */ @Test public void testBcdToBinary() { int binary = BcdConversion.bcdToBinary(0x1234); assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234 } - /** - * Test the binaryToBcd method with a binary number. - */ @Test public void testBinaryToBcd() { int bcd = BcdConversion.binaryToBcd(1234); assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234 } - /** - * Test the bcdToBinary method with zero. - */ @Test public void testBcdToBinaryZero() { int binary = BcdConversion.bcdToBinary(0x0); assertEquals(0, binary); // BCD 0x0 should convert to binary 0 } - /** - * Test the binaryToBcd method with zero. - */ @Test public void testBinaryToBcdZero() { int bcd = BcdConversion.binaryToBcd(0); assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0 } - /** - * Test the bcdToBinary method with a single digit BCD number. - */ @Test public void testBcdToBinarySingleDigit() { int binary = BcdConversion.bcdToBinary(0x7); assertEquals(7, binary); // BCD 0x7 should convert to binary 7 } - /** - * Test the binaryToBcd method with a single digit binary number. - */ @Test public void testBinaryToBcdSingleDigit() { int bcd = BcdConversion.binaryToBcd(7); assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7 } + + @Test + public void testBcdToBinaryMaxValue() { + int binary = BcdConversion.bcdToBinary(0x9999); + assertEquals(9999, binary); // BCD 0x9999 should convert to binary 9999 + } + + @Test + public void testBinaryToBcdMaxValue() { + int bcd = BcdConversion.binaryToBcd(9999); + assertEquals(0x9999, bcd); // Binary 9999 should convert to BCD 0x9999 + } + + @Test + public void testBcdToBinaryInvalidHighDigit() { + // Testing invalid BCD input where one of the digits is > 9 + assertThrows(IllegalArgumentException.class, () -> { + BcdConversion.bcdToBinary(0x123A); // Invalid BCD, 'A' is not a valid digit + }); + } + + @Test + public void testBinaryToBcdInvalidValue() { + // Testing conversion for numbers greater than 9999, which cannot be represented in BCD + assertThrows(IllegalArgumentException.class, () -> { + BcdConversion.binaryToBcd(10000); // 10000 is too large for BCD representation + }); + } + + @Test + public void testBcdToBinaryLeadingZeroes() { + int binary = BcdConversion.bcdToBinary(0x0234); + assertEquals(234, binary); // BCD 0x0234 should convert to binary 234, ignoring leading zero + } + + @Test + public void testBinaryToBcdLeadingZeroes() { + int bcd = BcdConversion.binaryToBcd(234); + assertEquals(0x0234, bcd); // Binary 234 should convert to BCD 0x0234 + } } From 8ca571fb619244cecd8c24ac1ee78449e9dc0cfc Mon Sep 17 00:00:00 2001 From: Hardvan Date: Sun, 13 Oct 2024 07:28:20 +0000 Subject: [PATCH 2/5] Update directory --- DIRECTORY.md | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index af956a1a26ed..42d119983918 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -26,9 +26,11 @@ * [BcdConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java) * [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java) * [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java) + * [BooleanAlgebraGates](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGates.java) * [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java) * [CountLeadingZeros](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountLeadingZeros.java) * [CountSetBits](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/CountSetBits.java) + * [FindNthBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/FindNthBit.java) * [GrayCodeConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/GrayCodeConversion.java) * [HammingDistance](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HammingDistance.java) * [HigherLowerPowerOfTwo](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwo.java) @@ -55,6 +57,7 @@ * [CompositeLFSR](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/CompositeLFSR.java) * [LFSR](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/LFSR.java) * [Utils](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/a5/Utils.java) + * [ADFGVXCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/ADFGVXCipher.java) * [AES](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AES.java) * [AESEncryption](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AESEncryption.java) * [AffineCipher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/ciphers/AffineCipher.java) @@ -134,6 +137,7 @@ * [FordFulkerson](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/FordFulkerson.java) * [Graphs](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Graphs.java) * [HamiltonianCycle](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/HamiltonianCycle.java) + * [JohnsonsAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithm.java) * [KahnsAlgorithm](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/KahnsAlgorithm.java) * [Kosaraju](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Kosaraju.java) * [Kruskal](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/datastructures/graphs/Kruskal.java) @@ -341,6 +345,7 @@ * [EulersFunction](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/EulersFunction.java) * [Factorial](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/Factorial.java) * [FactorialRecursion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FactorialRecursion.java) + * [FastExponentiation](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FastExponentiation.java) * [FastInverseSqrt](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FastInverseSqrt.java) * [FFT](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFT.java) * [FFTBluestein](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/maths/FFTBluestein.java) @@ -658,9 +663,11 @@ * [BcdConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java) * [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java) * [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java) + * [BooleanAlgebraGatesTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BooleanAlgebraGatesTest.java) * [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java) * [CountLeadingZerosTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountLeadingZerosTest.java) * [CountSetBitsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/CountSetBitsTest.java) + * [FindNthBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/FindNthBitTest.java) * [GrayCodeConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/GrayCodeConversionTest.java) * [HammingDistanceTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HammingDistanceTest.java) * [HigherLowerPowerOfTwoTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/HigherLowerPowerOfTwoTest.java) @@ -684,6 +691,7 @@ * [A5CipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5CipherTest.java) * [A5KeyStreamGeneratorTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/A5KeyStreamGeneratorTest.java) * [LFSRTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/a5/LFSRTest.java) + * [ADFGVXCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/ADFGVXCipherTest.java) * [AESEncryptionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AESEncryptionTest.java) * [AffineCipherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AffineCipherTest.java) * [AtbashTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/ciphers/AtbashTest.java) @@ -752,6 +760,7 @@ * [FloydWarshallTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FloydWarshallTest.java) * [FordFulkersonTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/FordFulkersonTest.java) * [HamiltonianCycleTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/HamiltonianCycleTest.java) + * [JohnsonsAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/JohnsonsAlgorithmTest.java) * [KosarajuTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/KosarajuTest.java) * [TarjansAlgorithmTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/TarjansAlgorithmTest.java) * [WelshPowellTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/graphs/WelshPowellTest.java) @@ -904,6 +913,7 @@ * [EulersFunctionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/EulersFunctionTest.java) * [FactorialRecursionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialRecursionTest.java) * [FactorialTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FactorialTest.java) + * [FastExponentiationTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastExponentiationTest.java) * [FastInverseSqrtTests](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FastInverseSqrtTests.java) * [FFTTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FFTTest.java) * [FibonacciJavaStreamsTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/maths/FibonacciJavaStreamsTest.java) From 4a436c9afaf8416367f9f0aca105da4b5e9493b2 Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 12:59:07 +0530 Subject: [PATCH 3/5] Fix --- .../bitmanipulation/BcdConversion.java | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java index 2275893fc1cf..536e68f60088 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java @@ -24,19 +24,26 @@ private BcdConversion() { /** * Converts a BCD (Binary-Coded Decimal) number to binary. *

Steps: - *

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

2. Multiply the extracted digit by the corresponding power of 10 and add it to the binary number. - *

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

4. Repeat steps 1-3 until the BCD number is zero. + *

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 binary 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 binary number. + * @throws IllegalArgumentException if the BCD number contains invalid digits. */ public static int bcdToBinary(int bcd) { int binary = 0; int multiplier = 1; + + // Validate BCD digits while (bcd > 0) { int digit = bcd & 0xF; + if (digit > 9) { + throw new IllegalArgumentException("Invalid BCD digit: " + digit); + } binary += digit * multiplier; multiplier *= 10; bcd >>= 4; From 804bfd4f0e035cfd40a680e18e5f33668473eb5f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 12:59:33 +0530 Subject: [PATCH 4/5] Fix --- .../bitmanipulation/BcdConversion.java | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java index 536e68f60088..25ab6f824059 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java @@ -54,15 +54,21 @@ public static int bcdToBinary(int bcd) { /** * Converts a binary number to BCD (Binary-Coded Decimal). *

Steps: - *

1. Extract the last decimal digit from the binary number. - *

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

3. Remove the last decimal digit from the binary number. - *

4. Repeat steps 1-3 until the binary number is zero. + *

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

2. Extract the last decimal digit from the binary 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 binary number. + *

5. Repeat steps 2-4 until the binary number is zero. * * @param binary The binary number. * @return The corresponding BCD number. + * @throws IllegalArgumentException if the binary number is greater than 9999. */ public static int binaryToBcd(int binary) { + if (binary < 0 || binary > 9999) { + throw new IllegalArgumentException("Value out of bounds for BCD representation: " + binary); + } + int bcd = 0; int shift = 0; while (binary > 0) { From 390b83db12277e4fce2b658392d80fc02939009f Mon Sep 17 00:00:00 2001 From: Hardik Pawar Date: Sun, 13 Oct 2024 13:04:40 +0530 Subject: [PATCH 5/5] Replaced binary with decimal --- .../bitmanipulation/BcdConversion.java | 50 +++++++------- .../bitmanipulation/BcdConversionTest.java | 68 +++++++++---------- 2 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java index 25ab6f824059..e6bd35720d9f 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java @@ -1,7 +1,7 @@ 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. * * 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. * @@ -10,11 +10,11 @@ * * Example usage: *

- * 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
  * 
*/ public final class BcdConversion { @@ -22,20 +22,20 @@ private BcdConversion() { } /** - * Converts a BCD (Binary-Coded Decimal) number to binary. + * 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 binary 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 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 @@ -44,37 +44,37 @@ public static int bcdToBinary(int bcd) { if (digit > 9) { throw new IllegalArgumentException("Invalid BCD digit: " + digit); } - binary += digit * multiplier; + decimal += digit * multiplier; multiplier *= 10; bcd >>= 4; } - return binary; + return decimal; } /** - * Converts a binary number to BCD (Binary-Coded Decimal). + * Converts a decimal number to BCD (Binary-Coded Decimal). *

Steps: - *

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

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

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 binary number. - *

5. Repeat steps 2-4 until the binary number is zero. + *

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

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 binary number is greater than 9999. + * @throws IllegalArgumentException if the decimal number is greater than 9999. */ - public static int binaryToBcd(int binary) { - if (binary < 0 || binary > 9999) { - throw new IllegalArgumentException("Value out of bounds for BCD representation: " + 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; + while (decimal > 0) { + int digit = decimal % 10; bcd |= (digit << (shift * 4)); - binary /= 10; + decimal /= 10; shift++; } return bcd; diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java index 70d6b58fb240..727b07eb9065 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java @@ -8,78 +8,78 @@ public class BcdConversionTest { @Test - public void testBcdToBinary() { - int binary = BcdConversion.bcdToBinary(0x1234); - assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234 + public void testBcdToDecimal() { + int decimal = BcdConversion.bcdToDecimal(0x1234); + assertEquals(1234, decimal); // BCD 0x1234 should convert to decimal 1234 } @Test - public void testBinaryToBcd() { - int bcd = BcdConversion.binaryToBcd(1234); - assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234 + public void testDecimalToBcd() { + int bcd = BcdConversion.decimalToBcd(1234); + assertEquals(0x1234, bcd); // Decimal 1234 should convert to BCD 0x1234 } @Test - public void testBcdToBinaryZero() { - int binary = BcdConversion.bcdToBinary(0x0); - assertEquals(0, binary); // BCD 0x0 should convert to binary 0 + public void testBcdToDecimalZero() { + int decimal = BcdConversion.bcdToDecimal(0x0); + assertEquals(0, decimal); // BCD 0x0 should convert to decimal 0 } @Test - public void testBinaryToBcdZero() { - int bcd = BcdConversion.binaryToBcd(0); - assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0 + public void testDecimalToBcdZero() { + int bcd = BcdConversion.decimalToBcd(0); + assertEquals(0x0, bcd); // Decimal 0 should convert to BCD 0x0 } @Test - public void testBcdToBinarySingleDigit() { - int binary = BcdConversion.bcdToBinary(0x7); - assertEquals(7, binary); // BCD 0x7 should convert to binary 7 + public void testBcdToDecimalSingleDigit() { + int decimal = BcdConversion.bcdToDecimal(0x7); + assertEquals(7, decimal); // BCD 0x7 should convert to decimal 7 } @Test - public void testBinaryToBcdSingleDigit() { - int bcd = BcdConversion.binaryToBcd(7); - assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7 + public void testDecimalToBcdSingleDigit() { + int bcd = BcdConversion.decimalToBcd(7); + assertEquals(0x7, bcd); // Decimal 7 should convert to BCD 0x7 } @Test - public void testBcdToBinaryMaxValue() { - int binary = BcdConversion.bcdToBinary(0x9999); - assertEquals(9999, binary); // BCD 0x9999 should convert to binary 9999 + public void testBcdToDecimalMaxValue() { + int decimal = BcdConversion.bcdToDecimal(0x9999); + assertEquals(9999, decimal); // BCD 0x9999 should convert to decimal 9999 } @Test - public void testBinaryToBcdMaxValue() { - int bcd = BcdConversion.binaryToBcd(9999); - assertEquals(0x9999, bcd); // Binary 9999 should convert to BCD 0x9999 + public void testDecimalToBcdMaxValue() { + int bcd = BcdConversion.decimalToBcd(9999); + assertEquals(0x9999, bcd); // Decimal 9999 should convert to BCD 0x9999 } @Test - public void testBcdToBinaryInvalidHighDigit() { + public void testBcdToDecimalInvalidHighDigit() { // Testing invalid BCD input where one of the digits is > 9 assertThrows(IllegalArgumentException.class, () -> { - BcdConversion.bcdToBinary(0x123A); // Invalid BCD, 'A' is not a valid digit + BcdConversion.bcdToDecimal(0x123A); // Invalid BCD, 'A' is not a valid digit }); } @Test - public void testBinaryToBcdInvalidValue() { + public void testDecimalToBcdInvalidValue() { // Testing conversion for numbers greater than 9999, which cannot be represented in BCD assertThrows(IllegalArgumentException.class, () -> { - BcdConversion.binaryToBcd(10000); // 10000 is too large for BCD representation + BcdConversion.decimalToBcd(10000); // 10000 is too large for BCD representation }); } @Test - public void testBcdToBinaryLeadingZeroes() { - int binary = BcdConversion.bcdToBinary(0x0234); - assertEquals(234, binary); // BCD 0x0234 should convert to binary 234, ignoring leading zero + public void testBcdToDecimalLeadingZeroes() { + int decimal = BcdConversion.bcdToDecimal(0x0234); + assertEquals(234, decimal); // BCD 0x0234 should convert to decimal 234, ignoring leading zero } @Test - public void testBinaryToBcdLeadingZeroes() { - int bcd = BcdConversion.binaryToBcd(234); - assertEquals(0x0234, bcd); // Binary 234 should convert to BCD 0x0234 + public void testDecimalToBcdLeadingZeroes() { + int bcd = BcdConversion.decimalToBcd(234); + assertEquals(0x0234, bcd); // Decimal 234 should convert to BCD 0x0234 } }