1
- package com .thealgorithms .conversions ;
1
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
2
+ import org .junit .jupiter .api .Test ;
2
3
3
4
/**
4
5
* This class provides methods to convert a decimal number to a binary number.
@@ -8,10 +9,12 @@ final class DecimalToBinary {
8
9
private static final int DECIMAL_MULTIPLIER = 10 ;
9
10
10
11
private DecimalToBinary () {
12
+ // Prevent instantiation of utility class
11
13
}
12
14
13
15
/**
14
16
* Converts a decimal number to a binary number using a conventional algorithm.
17
+ *
15
18
* @param decimalNumber the decimal number to convert
16
19
* @return the binary representation of the decimal number
17
20
*/
@@ -31,6 +34,7 @@ public static int convertUsingConventionalAlgorithm(int decimalNumber) {
31
34
32
35
/**
33
36
* Converts a decimal number to a binary number using a bitwise algorithm.
37
+ *
34
38
* @param decimalNumber the decimal number to convert
35
39
* @return the binary representation of the decimal number
36
40
*/
@@ -39,11 +43,38 @@ public static int convertUsingBitwiseAlgorithm(int decimalNumber) {
39
43
int position = 1 ;
40
44
41
45
while (decimalNumber > 0 ) {
42
- int leastSignificantBit = decimalNumber & 1 ;
46
+ int leastSignificantBit = decimalNumber & 1 ; // Extract LSB using bitwise AND
43
47
binaryNumber += leastSignificantBit * position ;
44
48
position *= DECIMAL_MULTIPLIER ;
45
- decimalNumber >>= 1 ;
49
+ decimalNumber >>= 1 ; // Right shift the decimal number to move to the next bit
46
50
}
51
+
47
52
return binaryNumber ;
48
53
}
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
+ }
49
80
}
0 commit comments