mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Add BcdConversion algorithm (#5742)
This commit is contained in:
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user