Skip to content

Commit d80fd0c

Browse files
authored
refactor: DecimalToBinary (#5339)
1 parent 7c58b19 commit d80fd0c

File tree

2 files changed

+52
-45
lines changed

2 files changed

+52
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,49 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
53
/**
6-
* This class converts a Decimal number to a Binary number
4+
* This class provides methods to convert a decimal number to a binary number.
75
*/
86
final class DecimalToBinary {
7+
private static final int BINARY_BASE = 2;
8+
private static final int DECIMAL_MULTIPLIER = 10;
9+
910
private DecimalToBinary() {
1011
}
1112

1213
/**
13-
* Main Method
14-
*
15-
* @param args Command Line Arguments
14+
* Converts a decimal number to a binary number using a conventional algorithm.
15+
* @param decimalNumber the decimal number to convert
16+
* @return the binary representation of the decimal number
1617
*/
17-
public static void main(String[] args) {
18-
conventionalConversion();
19-
bitwiseConversion();
20-
}
18+
public static int convertUsingConventionalAlgorithm(int decimalNumber) {
19+
int binaryNumber = 0;
20+
int position = 1;
2121

22-
/**
23-
* This method converts a decimal number to a binary number using a
24-
* conventional algorithm.
25-
*/
26-
public static void conventionalConversion() {
27-
int n;
28-
int b = 0;
29-
int c = 0;
30-
int d;
31-
Scanner input = new Scanner(System.in);
32-
System.out.printf("Conventional conversion.%n Enter the decimal number: ");
33-
n = input.nextInt();
34-
while (n != 0) {
35-
d = n % 2;
36-
b = b + d * (int) Math.pow(10, c++);
37-
n /= 2;
38-
} // converting decimal to binary
39-
System.out.println("\tBinary number: " + b);
40-
input.close();
22+
while (decimalNumber > 0) {
23+
int remainder = decimalNumber % BINARY_BASE;
24+
binaryNumber += remainder * position;
25+
position *= DECIMAL_MULTIPLIER;
26+
decimalNumber /= BINARY_BASE;
27+
}
28+
29+
return binaryNumber;
4130
}
4231

4332
/**
44-
* This method converts a decimal number to a binary number using a bitwise
45-
* algorithm
33+
* Converts a decimal number to a binary number using a bitwise algorithm.
34+
* @param decimalNumber the decimal number to convert
35+
* @return the binary representation of the decimal number
4636
*/
47-
public static void bitwiseConversion() {
48-
int n;
49-
int b = 0;
50-
int c = 0;
51-
int d;
52-
Scanner input = new Scanner(System.in);
53-
System.out.printf("Bitwise conversion.%n Enter the decimal number: ");
54-
n = input.nextInt();
55-
while (n != 0) {
56-
d = (n & 1);
57-
b += d * (int) Math.pow(10, c++);
58-
n >>= 1;
37+
public static int convertUsingBitwiseAlgorithm(int decimalNumber) {
38+
int binaryNumber = 0;
39+
int position = 1;
40+
41+
while (decimalNumber > 0) {
42+
int leastSignificantBit = decimalNumber & 1;
43+
binaryNumber += leastSignificantBit * position;
44+
position *= DECIMAL_MULTIPLIER;
45+
decimalNumber >>= 1;
5946
}
60-
System.out.println("\tBinary number: " + b);
61-
input.close();
47+
return binaryNumber;
6248
}
6349
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.conversions;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
public class DecimalToBinaryTest {
9+
10+
@ParameterizedTest
11+
@CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"})
12+
void testConvertUsingConventionalAlgorithm(int decimal, int expectedBinary) {
13+
assertEquals(expectedBinary, DecimalToBinary.convertUsingConventionalAlgorithm(decimal));
14+
}
15+
16+
@ParameterizedTest
17+
@CsvSource({"0, 0", "1, 1", "2, 10", "5, 101", "10, 1010", "15, 1111", "100, 1100100"})
18+
void testConvertUsingBitwiseAlgorithm(int decimal, int expectedBinary) {
19+
assertEquals(expectedBinary, DecimalToBinary.convertUsingBitwiseAlgorithm(decimal));
20+
}
21+
}

0 commit comments

Comments
 (0)