Skip to content

Commit 894c067

Browse files
authored
Update DecimalToBinary.java
Added the JUnit test in this.
1 parent 6682c7c commit 894c067

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/main/java/com/thealgorithms/conversions/DecimalToBinary.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
package com.thealgorithms.conversions;
1+
import static org.junit.jupiter.api.Assertions.assertEquals;
2+
import org.junit.jupiter.api.Test;
23

34
/**
45
* This class provides methods to convert a decimal number to a binary number.
@@ -8,10 +9,12 @@ final class DecimalToBinary {
89
private static final int DECIMAL_MULTIPLIER = 10;
910

1011
private DecimalToBinary() {
12+
// Prevent instantiation of utility class
1113
}
1214

1315
/**
1416
* Converts a decimal number to a binary number using a conventional algorithm.
17+
*
1518
* @param decimalNumber the decimal number to convert
1619
* @return the binary representation of the decimal number
1720
*/
@@ -31,6 +34,7 @@ public static int convertUsingConventionalAlgorithm(int decimalNumber) {
3134

3235
/**
3336
* Converts a decimal number to a binary number using a bitwise algorithm.
37+
*
3438
* @param decimalNumber the decimal number to convert
3539
* @return the binary representation of the decimal number
3640
*/
@@ -39,11 +43,38 @@ public static int convertUsingBitwiseAlgorithm(int decimalNumber) {
3943
int position = 1;
4044

4145
while (decimalNumber > 0) {
42-
int leastSignificantBit = decimalNumber & 1;
46+
int leastSignificantBit = decimalNumber & 1; // Extract LSB using bitwise AND
4347
binaryNumber += leastSignificantBit * position;
4448
position *= DECIMAL_MULTIPLIER;
45-
decimalNumber >>= 1;
49+
decimalNumber >>= 1; // Right shift the decimal number to move to the next bit
4650
}
51+
4752
return binaryNumber;
4853
}
54+
55+
/**
56+
* Unit tests for DecimalToBinary conversions.
57+
*/
58+
public static class DecimalToBinaryTest {
59+
60+
@Test
61+
void testConvertUsingConventionalAlgorithm() {
62+
// Testing conversion using conventional method
63+
assertEquals(1101, DecimalToBinary.convertUsingConventionalAlgorithm(13)); // 13 in binary is 1101
64+
assertEquals(1010, DecimalToBinary.convertUsingConventionalAlgorithm(10)); // 10 in binary is 1010
65+
assertEquals(1, DecimalToBinary.convertUsingConventionalAlgorithm(1)); // 1 in binary is 1
66+
assertEquals(0, DecimalToBinary.convertUsingConventionalAlgorithm(0)); // 0 in binary is 0
67+
assertEquals(10010, DecimalToBinary.convertUsingConventionalAlgorithm(18)); // 18 in binary is 10010
68+
}
69+
70+
@Test
71+
void testConvertUsingBitwiseAlgorithm() {
72+
// Testing conversion using bitwise method
73+
assertEquals(1101, DecimalToBinary.convertUsingBitwiseAlgorithm(13)); // 13 in binary is 1101
74+
assertEquals(1010, DecimalToBinary.convertUsingBitwiseAlgorithm(10)); // 10 in binary is 1010
75+
assertEquals(1, DecimalToBinary.convertUsingBitwiseAlgorithm(1)); // 1 in binary is 1
76+
assertEquals(0, DecimalToBinary.convertUsingBitwiseAlgorithm(0)); // 0 in binary is 0
77+
assertEquals(10010, DecimalToBinary.convertUsingBitwiseAlgorithm(18)); // 18 in binary is 10010
78+
}
79+
}
4980
}

0 commit comments

Comments
 (0)