Skip to content

Commit 58bd4b4

Browse files
authored
Update DecimalToBinary.java
1 parent 894c067 commit 58bd4b4

File tree

1 file changed

+18
-23
lines changed

1 file changed

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

5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
47
/**
58
* This class provides methods to convert a decimal number to a binary number.
69
*/
@@ -9,12 +12,12 @@ final class DecimalToBinary {
912
private static final int DECIMAL_MULTIPLIER = 10;
1013

1114
private DecimalToBinary() {
12-
// Prevent instantiation of utility class
15+
// Private constructor to prevent instantiation
1316
}
1417

1518
/**
1619
* Converts a decimal number to a binary number using a conventional algorithm.
17-
*
20+
*
1821
* @param decimalNumber the decimal number to convert
1922
* @return the binary representation of the decimal number
2023
*/
@@ -34,7 +37,7 @@ public static int convertUsingConventionalAlgorithm(int decimalNumber) {
3437

3538
/**
3639
* Converts a decimal number to a binary number using a bitwise algorithm.
37-
*
40+
*
3841
* @param decimalNumber the decimal number to convert
3942
* @return the binary representation of the decimal number
4043
*/
@@ -43,38 +46,30 @@ public static int convertUsingBitwiseAlgorithm(int decimalNumber) {
4346
int position = 1;
4447

4548
while (decimalNumber > 0) {
46-
int leastSignificantBit = decimalNumber & 1; // Extract LSB using bitwise AND
49+
int leastSignificantBit = decimalNumber & 1;
4750
binaryNumber += leastSignificantBit * position;
4851
position *= DECIMAL_MULTIPLIER;
49-
decimalNumber >>= 1; // Right shift the decimal number to move to the next bit
52+
decimalNumber >>= 1;
5053
}
51-
5254
return binaryNumber;
5355
}
5456

55-
/**
56-
* Unit tests for DecimalToBinary conversions.
57-
*/
57+
// Unit tests
5858
public static class DecimalToBinaryTest {
59-
6059
@Test
6160
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));
6865
}
6966

7067
@Test
7168
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));
7873
}
7974
}
8075
}

0 commit comments

Comments
 (0)