1
- import static org .junit .jupiter .api .Assertions .assertEquals ;
1
+ package com .thealgorithms .conversions ;
2
+
2
3
import org .junit .jupiter .api .Test ;
3
4
5
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
6
+
4
7
/**
5
8
* This class provides methods to convert a decimal number to a binary number.
6
9
*/
@@ -9,12 +12,12 @@ final class DecimalToBinary {
9
12
private static final int DECIMAL_MULTIPLIER = 10 ;
10
13
11
14
private DecimalToBinary () {
12
- // Prevent instantiation of utility class
15
+ // Private constructor to prevent instantiation
13
16
}
14
17
15
18
/**
16
19
* Converts a decimal number to a binary number using a conventional algorithm.
17
- *
20
+ *
18
21
* @param decimalNumber the decimal number to convert
19
22
* @return the binary representation of the decimal number
20
23
*/
@@ -34,7 +37,7 @@ public static int convertUsingConventionalAlgorithm(int decimalNumber) {
34
37
35
38
/**
36
39
* Converts a decimal number to a binary number using a bitwise algorithm.
37
- *
40
+ *
38
41
* @param decimalNumber the decimal number to convert
39
42
* @return the binary representation of the decimal number
40
43
*/
@@ -43,38 +46,30 @@ public static int convertUsingBitwiseAlgorithm(int decimalNumber) {
43
46
int position = 1 ;
44
47
45
48
while (decimalNumber > 0 ) {
46
- int leastSignificantBit = decimalNumber & 1 ; // Extract LSB using bitwise AND
49
+ int leastSignificantBit = decimalNumber & 1 ;
47
50
binaryNumber += leastSignificantBit * position ;
48
51
position *= DECIMAL_MULTIPLIER ;
49
- decimalNumber >>= 1 ; // Right shift the decimal number to move to the next bit
52
+ decimalNumber >>= 1 ;
50
53
}
51
-
52
54
return binaryNumber ;
53
55
}
54
56
55
- /**
56
- * Unit tests for DecimalToBinary conversions.
57
- */
57
+ // Unit tests
58
58
public static class DecimalToBinaryTest {
59
-
60
59
@ Test
61
60
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
61
+ assertEquals (101 , convertUsingConventionalAlgorithm (5 ));
62
+ assertEquals (111 , convertUsingConventionalAlgorithm (7 ));
63
+ assertEquals (1101 , convertUsingConventionalAlgorithm (13 ));
64
+ assertEquals (0 , convertUsingConventionalAlgorithm (0 ));
68
65
}
69
66
70
67
@ Test
71
68
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
69
+ assertEquals (101 , convertUsingBitwiseAlgorithm (5 ));
70
+ assertEquals (111 , convertUsingBitwiseAlgorithm (7 ));
71
+ assertEquals (1101 , convertUsingBitwiseAlgorithm (13 ));
72
+ assertEquals (0 , convertUsingBitwiseAlgorithm (0 ));
78
73
}
79
74
}
80
75
}
0 commit comments