From d7231e88a06470ee865332eb2db77b7344a0d768 Mon Sep 17 00:00:00 2001 From: Tanmay Singh Date: Sun, 13 Oct 2024 02:28:51 +0530 Subject: [PATCH] feat: Add BcdConversion algo with JUnit tests --- DIRECTORY.md | 2 + .../bitmanipulation/BcdConversion.java | 58 +++++++++++++++++ .../bitmanipulation/BcdConversionTest.java | 65 +++++++++++++++++++ 3 files changed, 125 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java diff --git a/DIRECTORY.md b/DIRECTORY.md index 30fa2cbee199..af956a1a26ed 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -23,6 +23,7 @@ * [WordPatternMatcher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java) * [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java) * bitmanipulation + * [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) * [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java) @@ -654,6 +655,7 @@ * [WordPatternMatcherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordPatternMatcherTest.java) * [WordSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java) * bitmanipulation + * [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) * [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java new file mode 100644 index 000000000000..3cf8411816c7 --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java @@ -0,0 +1,58 @@ +package com.thealgorithms.bitmanipulation; + +/** + * 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. + * + * For more information, refer to the + * Binary-Coded Decimal Wikipedia page. + * + * Example usage: + *
+ * int binary = BcdConversion.bcdToBinary(0x1234);
+ * System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
+ *
+ * int bcd = BcdConversion.binaryToBcd(1234);
+ * System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
+ * 
+ */ +public final class BcdConversion { + private BcdConversion() { + } + /** + * Converts a BCD (Binary-Coded Decimal) number to binary. + * + * @param bcd The BCD number. + * @return The corresponding binary number. + */ + 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) + binary += digit * multiplier; + multiplier *= 10; + bcd >>= 4; // Shift right by 4 bits to process the next BCD digit + } + return binary; + } + + /** + * Converts a binary number to BCD (Binary-Coded Decimal). + * + * @param binary The binary number. + * @return The corresponding BCD number. + */ + 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 + shift++; + } + return bcd; + } +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java new file mode 100644 index 000000000000..85221d92ad42 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java @@ -0,0 +1,65 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +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 + } +}