Skip to content

Commit 6682c7c

Browse files
Add BcdConversion algorithm (#5742)
1 parent 4a03f42 commit 6682c7c

File tree

3 files changed

+125
-0
lines changed

3 files changed

+125
-0
lines changed

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* [WordPatternMatcher](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordPatternMatcher.java)
2424
* [WordSearch](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/backtracking/WordSearch.java)
2525
* bitmanipulation
26+
* [BcdConversion](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java)
2627
* [BinaryPalindromeCheck](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheck.java)
2728
* [BitSwap](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/BitSwap.java)
2829
* [ClearLeftmostSetBit](https://github.com/TheAlgorithms/Java/blob/master/src/main/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBit.java)
@@ -654,6 +655,7 @@
654655
* [WordPatternMatcherTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordPatternMatcherTest.java)
655656
* [WordSearchTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/backtracking/WordSearchTest.java)
656657
* bitmanipulation
658+
* [BcdConversionTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java)
657659
* [BinaryPalindromeCheckTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BinaryPalindromeCheckTest.java)
658660
* [BitSwapTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/BitSwapTest.java)
659661
* [ClearLeftmostSetBitTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/bitmanipulation/ClearLeftmostSetBitTest.java)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
/**
4+
* This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
5+
*
6+
* 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.
7+
*
8+
* For more information, refer to the
9+
* <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
10+
*
11+
* <b>Example usage:</b>
12+
* <pre>
13+
* int binary = BcdConversion.bcdToBinary(0x1234);
14+
* System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
15+
*
16+
* int bcd = BcdConversion.binaryToBcd(1234);
17+
* System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
18+
* </pre>
19+
*/
20+
public final class BcdConversion {
21+
private BcdConversion() {
22+
}
23+
/**
24+
* Converts a BCD (Binary-Coded Decimal) number to binary.
25+
*
26+
* @param bcd The BCD number.
27+
* @return The corresponding binary number.
28+
*/
29+
public static int bcdToBinary(int bcd) {
30+
int binary = 0;
31+
int multiplier = 1;
32+
while (bcd > 0) {
33+
int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit)
34+
binary += digit * multiplier;
35+
multiplier *= 10;
36+
bcd >>= 4; // Shift right by 4 bits to process the next BCD digit
37+
}
38+
return binary;
39+
}
40+
41+
/**
42+
* Converts a binary number to BCD (Binary-Coded Decimal).
43+
*
44+
* @param binary The binary number.
45+
* @return The corresponding BCD number.
46+
*/
47+
public static int binaryToBcd(int binary) {
48+
int bcd = 0;
49+
int shift = 0;
50+
while (binary > 0) {
51+
int digit = binary % 10; // Extract the last decimal digit
52+
bcd |= (digit << (shift * 4)); // Shift the digit to the correct BCD position
53+
binary /= 10; // Remove the last decimal digit
54+
shift++;
55+
}
56+
return bcd;
57+
}
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.thealgorithms.bitmanipulation;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
/**
8+
* Unit tests for the BcdConversion class.
9+
*/
10+
public class BcdConversionTest {
11+
12+
/**
13+
* Test the bcdToBinary method with a BCD number.
14+
*/
15+
@Test
16+
public void testBcdToBinary() {
17+
int binary = BcdConversion.bcdToBinary(0x1234);
18+
assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234
19+
}
20+
21+
/**
22+
* Test the binaryToBcd method with a binary number.
23+
*/
24+
@Test
25+
public void testBinaryToBcd() {
26+
int bcd = BcdConversion.binaryToBcd(1234);
27+
assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234
28+
}
29+
30+
/**
31+
* Test the bcdToBinary method with zero.
32+
*/
33+
@Test
34+
public void testBcdToBinaryZero() {
35+
int binary = BcdConversion.bcdToBinary(0x0);
36+
assertEquals(0, binary); // BCD 0x0 should convert to binary 0
37+
}
38+
39+
/**
40+
* Test the binaryToBcd method with zero.
41+
*/
42+
@Test
43+
public void testBinaryToBcdZero() {
44+
int bcd = BcdConversion.binaryToBcd(0);
45+
assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0
46+
}
47+
48+
/**
49+
* Test the bcdToBinary method with a single digit BCD number.
50+
*/
51+
@Test
52+
public void testBcdToBinarySingleDigit() {
53+
int binary = BcdConversion.bcdToBinary(0x7);
54+
assertEquals(7, binary); // BCD 0x7 should convert to binary 7
55+
}
56+
57+
/**
58+
* Test the binaryToBcd method with a single digit binary number.
59+
*/
60+
@Test
61+
public void testBinaryToBcdSingleDigit() {
62+
int bcd = BcdConversion.binaryToBcd(7);
63+
assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7
64+
}
65+
}

0 commit comments

Comments
 (0)