Skip to content

feat: Add BcdConversion algo with JUnit tests #5742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
* <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 bcd = BcdConversion.binaryToBcd(1234);
* System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
* </pre>
*/
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;
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}