Skip to content

Commit 7780738

Browse files
committed
Refactor BCD Conversion docs and add more tests
1 parent 6682c7c commit 7780738

File tree

2 files changed

+58
-27
lines changed

2 files changed

+58
-27
lines changed

src/main/java/com/thealgorithms/bitmanipulation/BcdConversion.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
55
*
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.
6+
* 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.
77
*
88
* For more information, refer to the
99
* <a href="https://en.wikipedia.org/wiki/Binary-coded_decimal">Binary-Coded Decimal</a> Wikipedia page.
@@ -20,8 +20,14 @@
2020
public final class BcdConversion {
2121
private BcdConversion() {
2222
}
23+
2324
/**
2425
* Converts a BCD (Binary-Coded Decimal) number to binary.
26+
* <p>Steps:
27+
* <p>1. Extract the last 4 bits (one BCD digit) from the BCD number.
28+
* <p>2. Multiply the extracted digit by the corresponding power of 10 and add it to the binary number.
29+
* <p>3. Shift the BCD number right by 4 bits to process the next BCD digit.
30+
* <p>4. Repeat steps 1-3 until the BCD number is zero.
2531
*
2632
* @param bcd The BCD number.
2733
* @return The corresponding binary number.
@@ -30,16 +36,21 @@ public static int bcdToBinary(int bcd) {
3036
int binary = 0;
3137
int multiplier = 1;
3238
while (bcd > 0) {
33-
int digit = bcd & 0xF; // Extract the last 4 bits (one BCD digit)
39+
int digit = bcd & 0xF;
3440
binary += digit * multiplier;
3541
multiplier *= 10;
36-
bcd >>= 4; // Shift right by 4 bits to process the next BCD digit
42+
bcd >>= 4;
3743
}
3844
return binary;
3945
}
4046

4147
/**
4248
* Converts a binary number to BCD (Binary-Coded Decimal).
49+
* <p>Steps:
50+
* <p>1. Extract the last decimal digit from the binary number.
51+
* <p>2. Shift the digit to the correct BCD position and add it to the BCD number.
52+
* <p>3. Remove the last decimal digit from the binary number.
53+
* <p>4. Repeat steps 1-3 until the binary number is zero.
4354
*
4455
* @param binary The binary number.
4556
* @return The corresponding BCD number.
@@ -48,9 +59,9 @@ public static int binaryToBcd(int binary) {
4859
int bcd = 0;
4960
int shift = 0;
5061
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
62+
int digit = binary % 10;
63+
bcd |= (digit << (shift * 4));
64+
binary /= 10;
5465
shift++;
5566
}
5667
return bcd;
Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,85 @@
11
package com.thealgorithms.bitmanipulation;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import org.junit.jupiter.api.Test;
67

7-
/**
8-
* Unit tests for the BcdConversion class.
9-
*/
108
public class BcdConversionTest {
119

12-
/**
13-
* Test the bcdToBinary method with a BCD number.
14-
*/
1510
@Test
1611
public void testBcdToBinary() {
1712
int binary = BcdConversion.bcdToBinary(0x1234);
1813
assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234
1914
}
2015

21-
/**
22-
* Test the binaryToBcd method with a binary number.
23-
*/
2416
@Test
2517
public void testBinaryToBcd() {
2618
int bcd = BcdConversion.binaryToBcd(1234);
2719
assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234
2820
}
2921

30-
/**
31-
* Test the bcdToBinary method with zero.
32-
*/
3322
@Test
3423
public void testBcdToBinaryZero() {
3524
int binary = BcdConversion.bcdToBinary(0x0);
3625
assertEquals(0, binary); // BCD 0x0 should convert to binary 0
3726
}
3827

39-
/**
40-
* Test the binaryToBcd method with zero.
41-
*/
4228
@Test
4329
public void testBinaryToBcdZero() {
4430
int bcd = BcdConversion.binaryToBcd(0);
4531
assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0
4632
}
4733

48-
/**
49-
* Test the bcdToBinary method with a single digit BCD number.
50-
*/
5134
@Test
5235
public void testBcdToBinarySingleDigit() {
5336
int binary = BcdConversion.bcdToBinary(0x7);
5437
assertEquals(7, binary); // BCD 0x7 should convert to binary 7
5538
}
5639

57-
/**
58-
* Test the binaryToBcd method with a single digit binary number.
59-
*/
6040
@Test
6141
public void testBinaryToBcdSingleDigit() {
6242
int bcd = BcdConversion.binaryToBcd(7);
6343
assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7
6444
}
45+
46+
@Test
47+
public void testBcdToBinaryMaxValue() {
48+
int binary = BcdConversion.bcdToBinary(0x9999);
49+
assertEquals(9999, binary); // BCD 0x9999 should convert to binary 9999
50+
}
51+
52+
@Test
53+
public void testBinaryToBcdMaxValue() {
54+
int bcd = BcdConversion.binaryToBcd(9999);
55+
assertEquals(0x9999, bcd); // Binary 9999 should convert to BCD 0x9999
56+
}
57+
58+
@Test
59+
public void testBcdToBinaryInvalidHighDigit() {
60+
// Testing invalid BCD input where one of the digits is > 9
61+
assertThrows(IllegalArgumentException.class, () -> {
62+
BcdConversion.bcdToBinary(0x123A); // Invalid BCD, 'A' is not a valid digit
63+
});
64+
}
65+
66+
@Test
67+
public void testBinaryToBcdInvalidValue() {
68+
// Testing conversion for numbers greater than 9999, which cannot be represented in BCD
69+
assertThrows(IllegalArgumentException.class, () -> {
70+
BcdConversion.binaryToBcd(10000); // 10000 is too large for BCD representation
71+
});
72+
}
73+
74+
@Test
75+
public void testBcdToBinaryLeadingZeroes() {
76+
int binary = BcdConversion.bcdToBinary(0x0234);
77+
assertEquals(234, binary); // BCD 0x0234 should convert to binary 234, ignoring leading zero
78+
}
79+
80+
@Test
81+
public void testBinaryToBcdLeadingZeroes() {
82+
int bcd = BcdConversion.binaryToBcd(234);
83+
assertEquals(0x0234, bcd); // Binary 234 should convert to BCD 0x0234
84+
}
6585
}

0 commit comments

Comments
 (0)