Skip to content

Commit 390b83d

Browse files
committed
Replaced binary with decimal
1 parent 804bfd4 commit 390b83d

File tree

2 files changed

+59
-59
lines changed

2 files changed

+59
-59
lines changed

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

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.thealgorithms.bitmanipulation;
22

33
/**
4-
* This class provides methods to convert between BCD (Binary-Coded Decimal) and binary.
4+
* This class provides methods to convert between BCD (Binary-Coded Decimal) and decimal numbers.
55
*
66
* 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
*
@@ -10,32 +10,32 @@
1010
*
1111
* <b>Example usage:</b>
1212
* <pre>
13-
* int binary = BcdConversion.bcdToBinary(0x1234);
14-
* System.out.println("BCD 0x1234 to binary: " + binary); // Output: 1234
13+
* int decimal = BcdConversion.bcdToDecimal(0x1234);
14+
* System.out.println("BCD 0x1234 to decimal: " + decimal); // Output: 1234
1515
*
16-
* int bcd = BcdConversion.binaryToBcd(1234);
17-
* System.out.println("Binary 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
16+
* int bcd = BcdConversion.decimalToBcd(1234);
17+
* System.out.println("Decimal 1234 to BCD: " + Integer.toHexString(bcd)); // Output: 0x1234
1818
* </pre>
1919
*/
2020
public final class BcdConversion {
2121
private BcdConversion() {
2222
}
2323

2424
/**
25-
* Converts a BCD (Binary-Coded Decimal) number to binary.
25+
* Converts a BCD (Binary-Coded Decimal) number to a decimal number.
2626
* <p>Steps:
2727
* <p>1. Validate the BCD number to ensure all digits are between 0 and 9.
2828
* <p>2. Extract the last 4 bits (one BCD digit) from the BCD number.
29-
* <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the binary number.
29+
* <p>3. Multiply the extracted digit by the corresponding power of 10 and add it to the decimal number.
3030
* <p>4. Shift the BCD number right by 4 bits to process the next BCD digit.
3131
* <p>5. Repeat steps 1-4 until the BCD number is zero.
3232
*
3333
* @param bcd The BCD number.
34-
* @return The corresponding binary number.
34+
* @return The corresponding decimal number.
3535
* @throws IllegalArgumentException if the BCD number contains invalid digits.
3636
*/
37-
public static int bcdToBinary(int bcd) {
38-
int binary = 0;
37+
public static int bcdToDecimal(int bcd) {
38+
int decimal = 0;
3939
int multiplier = 1;
4040

4141
// Validate BCD digits
@@ -44,37 +44,37 @@ public static int bcdToBinary(int bcd) {
4444
if (digit > 9) {
4545
throw new IllegalArgumentException("Invalid BCD digit: " + digit);
4646
}
47-
binary += digit * multiplier;
47+
decimal += digit * multiplier;
4848
multiplier *= 10;
4949
bcd >>= 4;
5050
}
51-
return binary;
51+
return decimal;
5252
}
5353

5454
/**
55-
* Converts a binary number to BCD (Binary-Coded Decimal).
55+
* Converts a decimal number to BCD (Binary-Coded Decimal).
5656
* <p>Steps:
57-
* <p>1. Check if the binary number is within the valid range for BCD (0 to 9999).
58-
* <p>2. Extract the last decimal digit from the binary number.
57+
* <p>1. Check if the decimal number is within the valid range for BCD (0 to 9999).
58+
* <p>2. Extract the last decimal digit from the decimal number.
5959
* <p>3. Shift the digit to the correct BCD position and add it to the BCD number.
60-
* <p>4. Remove the last decimal digit from the binary number.
61-
* <p>5. Repeat steps 2-4 until the binary number is zero.
60+
* <p>4. Remove the last decimal digit from the decimal number.
61+
* <p>5. Repeat steps 2-4 until the decimal number is zero.
6262
*
63-
* @param binary The binary number.
63+
* @param decimal The decimal number.
6464
* @return The corresponding BCD number.
65-
* @throws IllegalArgumentException if the binary number is greater than 9999.
65+
* @throws IllegalArgumentException if the decimal number is greater than 9999.
6666
*/
67-
public static int binaryToBcd(int binary) {
68-
if (binary < 0 || binary > 9999) {
69-
throw new IllegalArgumentException("Value out of bounds for BCD representation: " + binary);
67+
public static int decimalToBcd(int decimal) {
68+
if (decimal < 0 || decimal > 9999) {
69+
throw new IllegalArgumentException("Value out of bounds for BCD representation: " + decimal);
7070
}
7171

7272
int bcd = 0;
7373
int shift = 0;
74-
while (binary > 0) {
75-
int digit = binary % 10;
74+
while (decimal > 0) {
75+
int digit = decimal % 10;
7676
bcd |= (digit << (shift * 4));
77-
binary /= 10;
77+
decimal /= 10;
7878
shift++;
7979
}
8080
return bcd;

src/test/java/com/thealgorithms/bitmanipulation/BcdConversionTest.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,78 +8,78 @@
88
public class BcdConversionTest {
99

1010
@Test
11-
public void testBcdToBinary() {
12-
int binary = BcdConversion.bcdToBinary(0x1234);
13-
assertEquals(1234, binary); // BCD 0x1234 should convert to binary 1234
11+
public void testBcdToDecimal() {
12+
int decimal = BcdConversion.bcdToDecimal(0x1234);
13+
assertEquals(1234, decimal); // BCD 0x1234 should convert to decimal 1234
1414
}
1515

1616
@Test
17-
public void testBinaryToBcd() {
18-
int bcd = BcdConversion.binaryToBcd(1234);
19-
assertEquals(0x1234, bcd); // Binary 1234 should convert to BCD 0x1234
17+
public void testDecimalToBcd() {
18+
int bcd = BcdConversion.decimalToBcd(1234);
19+
assertEquals(0x1234, bcd); // Decimal 1234 should convert to BCD 0x1234
2020
}
2121

2222
@Test
23-
public void testBcdToBinaryZero() {
24-
int binary = BcdConversion.bcdToBinary(0x0);
25-
assertEquals(0, binary); // BCD 0x0 should convert to binary 0
23+
public void testBcdToDecimalZero() {
24+
int decimal = BcdConversion.bcdToDecimal(0x0);
25+
assertEquals(0, decimal); // BCD 0x0 should convert to decimal 0
2626
}
2727

2828
@Test
29-
public void testBinaryToBcdZero() {
30-
int bcd = BcdConversion.binaryToBcd(0);
31-
assertEquals(0x0, bcd); // Binary 0 should convert to BCD 0x0
29+
public void testDecimalToBcdZero() {
30+
int bcd = BcdConversion.decimalToBcd(0);
31+
assertEquals(0x0, bcd); // Decimal 0 should convert to BCD 0x0
3232
}
3333

3434
@Test
35-
public void testBcdToBinarySingleDigit() {
36-
int binary = BcdConversion.bcdToBinary(0x7);
37-
assertEquals(7, binary); // BCD 0x7 should convert to binary 7
35+
public void testBcdToDecimalSingleDigit() {
36+
int decimal = BcdConversion.bcdToDecimal(0x7);
37+
assertEquals(7, decimal); // BCD 0x7 should convert to decimal 7
3838
}
3939

4040
@Test
41-
public void testBinaryToBcdSingleDigit() {
42-
int bcd = BcdConversion.binaryToBcd(7);
43-
assertEquals(0x7, bcd); // Binary 7 should convert to BCD 0x7
41+
public void testDecimalToBcdSingleDigit() {
42+
int bcd = BcdConversion.decimalToBcd(7);
43+
assertEquals(0x7, bcd); // Decimal 7 should convert to BCD 0x7
4444
}
4545

4646
@Test
47-
public void testBcdToBinaryMaxValue() {
48-
int binary = BcdConversion.bcdToBinary(0x9999);
49-
assertEquals(9999, binary); // BCD 0x9999 should convert to binary 9999
47+
public void testBcdToDecimalMaxValue() {
48+
int decimal = BcdConversion.bcdToDecimal(0x9999);
49+
assertEquals(9999, decimal); // BCD 0x9999 should convert to decimal 9999
5050
}
5151

5252
@Test
53-
public void testBinaryToBcdMaxValue() {
54-
int bcd = BcdConversion.binaryToBcd(9999);
55-
assertEquals(0x9999, bcd); // Binary 9999 should convert to BCD 0x9999
53+
public void testDecimalToBcdMaxValue() {
54+
int bcd = BcdConversion.decimalToBcd(9999);
55+
assertEquals(0x9999, bcd); // Decimal 9999 should convert to BCD 0x9999
5656
}
5757

5858
@Test
59-
public void testBcdToBinaryInvalidHighDigit() {
59+
public void testBcdToDecimalInvalidHighDigit() {
6060
// Testing invalid BCD input where one of the digits is > 9
6161
assertThrows(IllegalArgumentException.class, () -> {
62-
BcdConversion.bcdToBinary(0x123A); // Invalid BCD, 'A' is not a valid digit
62+
BcdConversion.bcdToDecimal(0x123A); // Invalid BCD, 'A' is not a valid digit
6363
});
6464
}
6565

6666
@Test
67-
public void testBinaryToBcdInvalidValue() {
67+
public void testDecimalToBcdInvalidValue() {
6868
// Testing conversion for numbers greater than 9999, which cannot be represented in BCD
6969
assertThrows(IllegalArgumentException.class, () -> {
70-
BcdConversion.binaryToBcd(10000); // 10000 is too large for BCD representation
70+
BcdConversion.decimalToBcd(10000); // 10000 is too large for BCD representation
7171
});
7272
}
7373

7474
@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
75+
public void testBcdToDecimalLeadingZeroes() {
76+
int decimal = BcdConversion.bcdToDecimal(0x0234);
77+
assertEquals(234, decimal); // BCD 0x0234 should convert to decimal 234, ignoring leading zero
7878
}
7979

8080
@Test
81-
public void testBinaryToBcdLeadingZeroes() {
82-
int bcd = BcdConversion.binaryToBcd(234);
83-
assertEquals(0x0234, bcd); // Binary 234 should convert to BCD 0x0234
81+
public void testDecimalToBcdLeadingZeroes() {
82+
int bcd = BcdConversion.decimalToBcd(234);
83+
assertEquals(0x0234, bcd); // Decimal 234 should convert to BCD 0x0234
8484
}
8585
}

0 commit comments

Comments
 (0)